实验七 运算符重载

实验目的和要求

熟悉运算符重载的定义和使用方法 

实验内容

1.调试下列程序

[cpp]  view plain  copy
  1. //sy7_1.cpp    
  2. #include<iostream>    
  3. using namespace std;    
  4. class complex    
  5. {    
  6.     public:    
  7.         complex(){real=imag=0.0;}    
  8.         complex(double r){real=r;imag=0.0;}    
  9.         complex(double r,double i){real=r;imag=i;}    
  10.         complex operator + (const complex &c);    
  11.         complex operator - (const complex &c);    
  12.         complex operator * (const complex &c);    
  13.         complex operator / (const complex &c);    
  14.         friend void print(const complex &c);    
  15.     private:    
  16.         double real,imag;    
  17. };    
  18. inline complex complex::operator + (const complex &c)    
  19. {    
  20.     return complex(real+c.real,imag+c.imag);    
  21. }    
  22. inline complex complex::operator - (const complex &c)    
  23. {    
  24.     return complex(real-c.real,imag-c.imag);    
  25. }    
  26. inline complex complex::operator * (const complex &c)    
  27. {    
  28.     return complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);    
  29. }    
  30. inline complex complex::operator / (const complex &c)    
  31. {    
  32.     return complex((real*c.real+imag*c.imag)/(c.real*c.real+c.imag*c.imag),(imag*c.real-real*c.imag)/(c.real*c.real+c.imag*c.imag));    
  33. }    
  34. void print(const complex &c)    
  35. {    
  36.     if(c.imag<0)    
  37.         cout<<c.real<<c.imag<<"i";    
  38.     else    
  39.         cout<<c.real<<"+"<<c.imag<<"i";    
  40. }    
  41. int main()    
  42. {    
  43.     complex c1(2.0),c2(3.0,-1.0),c3;    
  44.     c3=c1+c2;    
  45.     cout<<"\nc1+c2= ";    
  46.     print(c3);    
  47.     c3=c1-c2;    
  48.     cout<<"\nc1-c2= ";    
  49.     print(c3);    
  50.     c3=c1*c2;    
  51.     cout<<"\nc1*c2= ";    
  52.     print(c3);    
  53.     c3=c1/c2;    
  54.     cout<<"\nc1/c2= ";    
  55.     print(c3);    
  56.     c3=(c1+c2)*(c1-c2)*c2/c1;    
  57.     cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";    
  58.     print(c3);    
  59.     cout<<endl;    
  60.     return 0;    
  61. }  

写出程序的输出结果,并解释

实验结果如下:


2.调试下列程序

[cpp]  view plain  copy
  1. //sy7_2.cpp  
  2. #include<iostream>  
  3. using namespace std;  
  4. class complex  
  5. {  
  6.     public:  
  7.         complex(){real=imag=0.0;}  
  8.         complex(double r){real=r;imag=0.0;}  
  9.         complex(double r,double i){real=r;imag=i;}  
  10.         friend complex operator + (const complex &c1,const complex &c2);  
  11.         friend complex operator - (const complex &c1,const complex &c2);  
  12.         friend complex operator * (const complex &c1,const complex &c2);  
  13.         friend complex operator / (const complex &c1,const complex &c2);  
  14.         friend void print(const complex &c);  
  15.     private:  
  16.         double real,imag;  
  17. };  
  18. complex operator + (const complex &c1,const complex &c2)  
  19. {  
  20.     return complex(c1.real+c2.real,c1.imag+c2.imag);  
  21. }  
  22. complex operator - (const complex &c1,const complex &c2)  
  23. {  
  24.     return complex(c1.real-c2.real,c1.imag-c2.imag);  
  25. }  
  26. complex operator * (const complex &c1,const complex &c2)  
  27. {  
  28.     return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);  
  29. }  
  30. complex operator / (const complex &c1,const complex &c2)  
  31. {  
  32.     return complex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));  
  33. }  
  34. void print(const complex &c)  
  35. {  
  36.     if(c.imag<0)  
  37.         cout<<c.real<<c.imag<<"i";  
  38.     else  
  39.         cout<<c.real<<"+"<<c.imag<<"i";  
  40. }  
  41. int main()  
  42. {  
  43.     complex c1(2.0),c2(3.0,-1.0),c3;  
  44.     c3=c1+c2;  
  45.     cout<<"\nc1+c2= ";  
  46.     print(c3);  
  47.     c3=c1-c2;  
  48.     cout<<"\nc1-c2= ";  
  49.     print(c3);  
  50.     c3=c1*c2;  
  51.     cout<<"\nc1*c2= ";  
  52.     print(c3);  
  53.     c3=c1/c2;  
  54.     cout<<"\nc1/c2= ";  
  55.     print(c3);  
  56.     c3=(c1+c2)*(c1-c2)*c2/c1;  
  57.     cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";  
  58.     print(c3);  
  59.     cout<<endl;  
  60.     return 0;  
  61. }  

写出程序的输出的结果,并解释。

程序结果如下;


3.定义一个Time类用来保存时间(时、分、秒),通过重载操作符“+”实现两个时间的相加(sy7-3.cpp)



分析与讨论

结合上题中的程序总结运算符重载的形式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值