c++训练营--重载
// demo1.cpp : 定义控制台应用程序的入口点。
//通过此例程了解重载
#include "stdafx.h"
#include <iostream>
using namespace std;
class CMath
{
public:
CMath(float a):m_a(a)
{
}
~CMath()
{
}
double Add(double a,double b);
double Sub(double a,double b);
double Mul(double a,double b);
double Div(double a,double b);
int Add(int a,int b)
{
return a+b;
}
int Sub(int a,int b)
{
return a-b;
}
int Mul(int a,int b)
{
return a*b;
}
int Div(int a,int b)
{
if (b!=0)
{
return a/b;
}
return 0;
}
public:
CMath operator +(CMath& mymath)
{
CMath result(0);
result.m_a=m_a+mymath.m_a;
return result;
}
CMath operator -(CMath& mymath)
{
CMath result(0);
result.m_a=m_a-mymath.m_a;
return result;
}
CMath operator *(CMath& mymath)
{
CMath result(0);
result.m_a=m_a*mymath.m_a;
return result;
}
CMath operator /(CMath& mymath)
{
if (mymath.m_a==0)
{
mymath.m_a=1;
}
CMath result(0);
result.m_a=m_a/mymath.m_a;
return result;
}
CMath operator ++()//前++
{
this->m_a+=1;
return *this;
}
CMath operator ++(int)//后++,加上參数int,无意义
{
CMath *t=this;
++(t->m_a);
return *t;
}
CMath operator --()//前--
{
this->m_a-=1;
return *this;
}
CMath operator --(int)//后--,加上參数int,无意义
{
CMath *t=this;
--(this->m_a);
return *t;
}
CMath operator +=(CMath & mymath)
{
m_a+=mymath.m_a;
return *this;
}
CMath operator -=(CMath & mymath)
{
m_a-=mymath.m_a;
return *this;
}
bool operator ==(CMath &mymath)
{
return (m_a==mymath.m_a);
}
friend ostream& operator <<(ostream &os,CMath &t);//必须是友元
friend iostream& operator >>(iostream &is,CMath &t);//必须是友元
CMath operator ()(const CMath& t)
{
return CMath(this->m_a=t.m_a);
}
CMath& operator =(CMath &t)
{
this->m_a=t.m_a;
return *this;
}
private:
float m_a;
};
ostream& operator <<(ostream &os,CMath &t)
{
os<<t.m_a<<endl;
return os;
}
iostream& operator >>(iostream &is,CMath &t)
{
is>>t.m_a;
return is;
}
double CMath::Add(double a,double b)
{
return a+b;
}
double CMath::Sub(double a,double b)
{
return a-b;
}
double CMath::Mul(double a,double b)
{
return a*b;
}
double CMath::Div(double a,double b)
{
if (b==0)
{
b=1;
}
return a/b;
}
int _tmain(int argc, _TCHAR* argv[])
{
CMath mymath(4);
cout<<mymath.Add(1.1,2.1)<<endl;//调用參数为double类型的Add函数
cout<<mymath.Add(1,2)<<endl;//调用參数为int类型的Add函数
cout<<mymath.Sub(1,2)<<endl;//
cout<<mymath.Mul(1,2)<<endl;
cout<<mymath.Div(1.3,2.3)<<endl;
cout<<endl<<endl;
cout<<"mymath:"<<mymath<<endl;
mymath++;
cout<<"mymath++:"<<mymath<<endl;
mymath--;
cout<<"mymath--:"<<mymath<<endl;
cout<<"mymath+mymath:"<<mymath+mymath<<endl;
cout<<"mymath*mymath:"<<mymath*mymath<<endl;
cout<<"mymath/mymath:"<<mymath/mymath<<endl;
CMath mymath1(20);
mymath=mymath1;
cout<<"mymath=mymath1:"<<mymath<<endl;
if (mymath==mymath1)
{
cout<<"mymath==mymath1"<<endl;
}
mymath1+=mymath;
cout<<"mymath1:"<<mymath1<<endl;
mymath1-=mymath;
cout<<"mymath1:"<<mymath1<<endl;
mymath--;
mymath1(mymath);
cout<<"mymath1(mymath):"<<mymath1<<endl;
return 0;
}
c++训练营--重载的更多相关文章
- .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]
方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...
- PHP类和对象之重载
PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...
- C#基础回顾(二)—页面值传递、重载与重写、类与结构体、装箱与拆箱
一.前言 -孤独的路上有梦想作伴,乘风破浪- 二.页面值传递 (1)C#各页面之间可以进行数据的交换和传递,页面之间可根据获取的数据,进行各自的操作(跳转.计算等操作).为了实现多种方式的数据传递,C ...
- new/delete重载
在c++中,有时我们需要在运行阶段为一个变量分配未命名的内存,并使用指针来访问它,这里就可以用到new关键字.另外需要指出的是,new分配的内存块通常与常规变量分配的内存块不同,常规变量的值都储存在被 ...
- java重载与覆写
很多同学对于overload和override傻傻分不清楚,建议不要死记硬背概念性的知识,要理解着去记忆. 先给出我的定义: overload(重载):在同一类或者有着继承关系的类中,一组名称相同,参 ...
- 【C++】多态性(函数重载与虚函数)
多态性就是同一符号或名字在不同情况下具有不同解释的现象.多态性有两种表现形式: 编译时多态性:同一对象收到相同的消息却产生不同的函数调用,一般通过函数重载来实现,在编译时就实现了绑定,属于静态绑定. ...
- C++ 运算符重载时,将运算符两边对象交换问题.
在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...
- C++重载new和delete运算符
内存管理运算符 new.new[].delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数.一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内 ...
- Java学习笔记之方法重载
被重载的方法必须具有不同的参数列表.不能基于不同修饰符或返回值类型来重载方法. package welcome; public class TestMethodOverloading { public ...
随机推荐
- 关于TCP/UDP缓存
1.修订单个socket的缓冲区大小:通过setsockopt使用SO_RCVBUF来设置接收缓冲区,该参数在设置的时候不会与rmem_max进行对比校验,但是如果设置的大小超过rmem_max的话, ...
- [Everyday Mathematics]20150303
设 $f$ 是 $\bbR$ 上的 $T$ - 周期函数, 试证: $$\bex \int_T^\infty\frac{f(x)}{x}\rd x\mbox{ 收敛 } \ra \int_0^T f( ...
- Android基于XMPP Smack openfire 开发的聊天室
Android基于XMPP Smack openfire 开发的聊天室(一)[会议服务.聊天室列表.加入] http://blog.csdn.net/lnb333666/article/details ...
- Android 中像素px和dp的转化
在Android的布局文件中,往往使用dp作为控件的宽度和高度尺寸,但是在Java代码中,调用getWidth()方法获得的尺寸单位却是像素px,这两个单位有明显的区别:dp和屏幕的密度有关,而px与 ...
- Google搜索的常用技巧
个人搜索方案 1.选择合适的搜索词,一些行业术语或专家名字可以带来更加高质量的结果. 2.搜索词手动使用空格分隔,先进行第一次搜索,看搜索结果标题是否满足预期,如果不满足,采用更换关键词,添加关键词, ...
- TLCL中英对照版
TLCL中英文对照阅读网址:http://billie66.github.io/TLCL/book/index.html 感谢好奇猫团队(http://haoqicat.com/about/team) ...
- SparkR grammer
They are different between local R and sparkR: sparkR 跑通的函数: http://blog.csdn.net/wa2003/article/det ...
- 新手须知设计的法则 Mark
经常看到一些讲如何学习设计的文章,坦白讲感觉有些千篇一律.且不痛不痒,都说要看点书.学点画.练软件.多观察……唉,练软件这事还要说么,难道你还需要告诉一个人学开发是需要学习编程语言的? 学习是基于过往 ...
- select多个字段赋值给多个变量
在存储过程中定义了变量v1 int;v2 int;v3 int;从表tab1选择3个字段f1,f2,f3赋值给这三个变量,要如何写 如果单个变量可以 select f1 into v1 from t ...
- Mapreduce读取Hbase表,写数据到多个Hbase表中
Job端的变化: 通过设置conf,配置输出表,在reduce中获取输出表名字 Configuration conf = job.getConfiguration(); //输出表1 conf.set ...