运算符重载
9.1 复数类对象的加法运算-用成员函数
利用成员函数实现复数类对象的加法运算。
#include <iostream>
using namespace std;
class complex //复数类声明
{
public: //外部接口
complex(double r=0.0,double i=0.0)
{real=r;imag=i;}
complex add(complex &c);
void Show();
private:
double real;
double imag;
};
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
complex complex::add(complex &c2)
{ complex t;
t.real=real+c2.real;
t.imag=imag+c2.imag;
return t;
}
int main() //主函数
{
double a,b,c,d;
cin>>a>>b>>c>>d;
complex c1(a,b),c2(c,d),c3; //声明复数类的对象
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
c3=c1.add(c2); //使用成员函数add完成复数加法
cout<<"c3=c1+c2=";
c3.Show();
return 0;
}
9.2 复数类对象的加法运算-用友元函数
利用非成员函数,即友元函数实现复数对象的加法运算
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0)
{real=r;imag=i;}
friend complex add(complex &c1,complex &c2);
void Show();
private:
double real;
double imag;
};
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
complex add(complex &c1,complex &c2)
{ complex t;
t.real=c1.real+c2.real;
t.imag=c1.imag+c2.imag;
return t;
}
int main() //主函数
{
double a,b,c,d;
cin>>a>>b>>c>>d;
complex c1(a,b),c2(c,d),c3; //声明复数类的对象
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
c3=add(c1,c2); //使用友元函数add完成复数加法
cout<<"c3=c1+c2=";
c3.Show();
return 0;
}
9.3 复数类对象的加法运算-运算符重载为成员函数
用+运算符实现复数类对象的加法运算,用重载+运算符为成员函数
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
complex add(complex &c2);
complex operator +(complex &c2);
void Show();
private:
double real;
double imag;
};
complex complex::operator +(complex &c2)
{ complex t;
t.real=real+c2.real;
t.imag=imag+c2.imag;
return t;
}
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main() //主函数
{
double a,b,c,d;
cin>>a>>b>>c>>d;
complex c1(a,b),c2(c,d),c3; //声明复数类的对象
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
c3=c1+c2; //使用重载运算符完成复数加法
cout<<"c3=c1+c2=";
c3.Show();
return 0;
}
9.4 复数类对象的加法运算-运算符重载为友元函数
用+运算符实现复数类对象的加法运算,用重载+运算符为友元函数
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
friend complex operator +(complex &c1,complex &c2);
void Show();
private:
double real;
double imag;
};
complex operator +(complex &c1,complex &c2)
{ complex t;
t.real=c1.real+c2.real;
t.imag=c1.imag+c2.imag;
return t;
}
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main() //主函数
{
double a,b,c,d;
cin>>a>>b>>c>>d;
complex c1(a,b),c2(c,d),c3; //声明复数类的对象
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
c3=c1+c2; //使用重载运算符完成复数加法
cout<<"c3=c1+c2=";
c3.Show();
return 0;
}
9.5 复数类对象的++运算-重载++运算符为成员函数
用++运算符实现复数对象的实部加1,用重载++为成员函数。
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
complex operator ++();
complex operator ++(int);
void Show();
private:
double real;
double imag;
};
complex complex::operator ++()
{ complex t;
t.real=real+1;
t.imag=imag;
real=real+1;
imag=imag;
return t;
}
complex complex::operator ++(int)
{ complex t;
t.real=real;
t.imag=imag;
real=real+1;
imag=imag;
return t;
}
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main() //主函数
{
double a,b,c,d;
cin>>a>>b>>c>>d;
complex c1(a,b),c2(c,d); //声明复数类的对象,c1,c2有参
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
++c1;//使用重载运算符++完成复数前置++
cout<<"after ++c1,c1=";
c1.Show();
c2++; //使用重载运算符++完成复数后置++
cout<<"after c2++,c2=";
c2.Show();
return 0;
}
9.6 复数类对象的++运算-重载++运算符为友元函数
用++运算符实现复数对象的实部加1,用重载++为友元函数。
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
friend complex operator ++(complex &c);
friend complex operator ++(complex &c,int);
void Show();
private:
double real;
double imag;
};
complex operator ++(complex &c)
{ complex t;
t.real=c.real+1;
t.imag=c.imag;
c.real=c.real+1;
c.imag=c.imag;
return t;
}
complex operator ++(complex &c,int)
{ complex t;
t.real=c.real;
t.imag=c.imag;
c.real=c.real+1;
c.imag=c.imag;
return t;
}
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main() //主函数
{
double a,b,c,d;
cin>>a>>b>>c>>d;
complex c1(a,b),c2(c,d); //声明复数类的对象,c1,c2有参
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
++c1;//使用重载运算符++完成复数前置++
cout<<"after ++c1,c1=";
c1.Show();
c2++; //使用重载运算符++完成复数后置++
cout<<"after c2++,c2=";
c2.Show();
return 0;
}
9.7 日期类设计与运算符重载
在Date类中,重载++运算符(前置、后置)为成员函数,使得在当前日期上加上1天形成一个新的日期。
#include <iostream>
using namespace std;
class Date
{
private:
int yr,mn,dy;
public:
void ShowMe()
{
cout<<yr<<"-"<<mn<<"-"<<dy<<endl;
}
Date operator ++();
Date operator ++(int);
Date(int a=0,int b=0,int c=0)
{
yr=a; mn=b; dy=c;
}
};
Date Date::operator ++()
{
if(mn==2)
{
if(dy>=28)
{
dy=1;
mn+=1;
}
}
else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10)
{
if(dy<31)
{
dy+=1;
}
else
{
dy=1;
mn+=1;
}
}
else if(mn==12)
{
if(dy<31)
{
dy+=1;
}
else
{
dy=1;
mn=1;
yr+=1;
}
}
else
{
if(dy<30)
{
dy+=1;
}
else
{
dy=1;
mn+=1;
}
}
Date d;
d.yr=yr;
d.mn=mn;
d.dy=dy;
yr=yr;
mn=mn;
dy=dy;
return d;
}
Date Date::operator ++(int)
{
if(mn==2)
{
if(dy>=28)
{
dy=1;
mn+=1;
}
}
else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10)
{
if(dy<31)
{
dy+=1;
}
else
{
dy=1;
mn+=1;
}
}
else if(mn==12)
{
if(dy<31)
{
dy+=1;
}
else
{
dy=1;
mn=1;
yr+=1;
}
}
else
{
if(dy<30)
{
dy+=1;
}
else
{
dy=1;
mn+=1;
}
}
Date d;
d.yr=yr;
d.mn=mn;
d.dy=dy;
yr=yr;
mn=mn;
dy=dy;
return d;
}
int main()
{
int a,b,c;
cin>>a>>b>>c;//从键盘输入年月日
Date x(a,b,c);//定义一个日期对象,带参数
x.ShowMe();//输出年月日
x++;//把当前日期改为第二天
x.ShowMe();//输出第二天的年月日
++x;//把当前日期改为第二天
x.ShowMe();//输出第二天的年月日
return 0;
}
9.8 复数的加法运算–成员函数重载
实现复数的加法运算,要求通过成员函数重载的方法实现2个复数对象的加运算、1个复数与1个实数的加运算。
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
complex add(complex &c2);
complex add(double e);
void Show();
private:
double real;
double imag;
};
complex complex::add(complex &c2)
{ complex t;
t.real=real+c2.real;
t.imag=imag+c2.imag;
return t;
}
complex complex::add(double e)
{
complex t;
t.real=real+e;
t.imag=imag;
return t;
}
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main() //主函数
{
double a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
complex c1(a,b),c2(c,d),c3; //声明复数类的对象
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
c3=c1.add(c2); //使用成员函数add完成c1和c2对象完成复数加法
cout<<"c3=c1+c2=";
c3.Show();
c3=c1.add(e);//使用成员函数add完成c1对象和实数e完成复数加法
cout<<"c3=c1+e=";
c3.Show();
return 0;
}
9.9 复数的加法运算-友元函数重载
实现复数的加法运算,要求通过友元函数重载的方法实现2个复数对象的加运算、1个复数与1个实数的加运算。
#include <iostream>
using namespace std;
class complex
{
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
friend complex add(complex &c1,complex &c2);
friend complex add(complex &c1,double e);
void Show();
private:
double real;
double imag;
};
complex add(complex &c1,complex &c2)
{ complex t;
t.real=c1.real+c2.real;
t.imag=c1.imag+c2.imag;
return t;
}
complex add(complex &c1,double e)
{
complex t;
t.real=c1.real+e;
t.imag=c1.imag;
return t;
}
void complex::Show()
{ cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main() //主函数
{
double a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
complex c1(a,b),c2(c,d),c3; //声明复数类的对象
cout<<"c1=";
c1.Show();
cout<<"c2=";
c2.Show();
c3=add(c1,c2); //使用友元函数add完成c1和c2对象完成复数加法
cout<<"c3=c1+c2=";
c3.Show();
c3=add(c1,e);//使用友元函数add完成c1对象和实数e完成复数加法
cout<<"c3=c1+e=";
c3.Show();
return 0;
}