分类: 信息化
2014-05-13 10:20:38
// demo1.cpp : 定义控制台应用程序的入口点。
//通过此例程了解重载
#include "stdafx.h"
#include
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<
}
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<
cout<
cout<
cout<<"mymath++:"<
cout<<"mymath--:"<
CMath mymath1(20);
mymath=mymath1;
cout<<"mymath=mymath1:"<
if (mymath==mymath1)
{
cout<<"mymath==mymath1"<
mymath1+=mymath;
cout<<"mymath1:"<
mymath1-=mymath;
cout<<"mymath1:"<
mymath--;
mymath1(mymath);
cout<<"mymath1(mymath):"<
return 0;
} 来源免费Q币资源网:http://blog.sina.com.cn/bugsqb