第七章实验

实验目的和要求

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

实验内容

1.调试下列程序

1. #include<iostream>  

2. using namespace std;  

3. class complex  

4. {  

5.     public:  

6.         complex(){real=imag=0.0;}  

7.         complex(double r){real=r;imag=0.0;}  

8.         complex(double r,double i){real=r;imag=i;}  

9.         complex operator + (const complex &c);  

10.         complex operator - (const complex &c);  

11.         complex operator * (const complex &c);  

12.         complex operator / (const complex &c);  

13.         friend void print(const complex &c);  

14.     private:  

15.         double real,imag;  

16. };  

17. inline complex complex::operator + (const complex &c)  

18. {  

19.     return complex(real+c.real,imag+c.imag);  

20. }  

21. inline complex complex::operator - (const complex &c)  

22. {  

23.     return complex(real-c.real,imag-c.imag);  

24. }  

25. inline complex complex::operator * (const complex &c)  

26. {  

27.     return complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);  

28. }  

29. inline complex complex::operator / (const complex &c)  

30. {  

31.     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));  

32. }  

33. void print(const complex &c)  

34. {  

35.     if(c.imag<0)  

36.         cout<<c.real<<c.imag<<"i";  

37.     else  

38.         cout<<c.real<<"+"<<c.imag<<"i";  

39. }  

40. int main()  

41. {  

42.     complex c1(2.0),c2(3.0,-1.0),c3;  

43.     c3=c1+c2;  

44.     cout<<"\nc1+c2= ";  

45.     print(c3);  

46.     c3=c1-c2;  

47.     cout<<"\nc1-c2= ";  

48.     print(c3);  

49.     c3=c1*c2;  

50.     cout<<"\nc1*c2= ";  

51.     print(c3);  

52.     c3=c1/c2;  

53.     cout<<"\nc1/c2= ";  

54.     print(c3);  

55.     c3=(c1+c2)*(c1-c2)*c2/c1;  

56.     cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";  

57.     print(c3);  

58.     cout<<endl;  

59.     return 0;  

60. }  

运行结果如下:

 

 

2.调试下列程序

1. #include<iostream>  

2. using namespace std;  

3. class complex  

4. {  

5.     public:  

6.         complex(){real=imag=0.0;}  

7.         complex(double r){real=r;imag=0.0;}  

8.         complex(double r,double i){real=r;imag=i;}  

9.         friend complex operator + (const complex &c1,const complex &c2);  

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 void print(const complex &c);  

14.     private:  

15.         double real,imag;  

16. };  

17. complex operator + (const complex &c1,const complex &c2)  

18. {  

19.     return complex(c1.real+c2.real,c1.imag+c2.imag);  

20. }  

21. complex operator - (const complex &c1,const complex &c2)  

22. {  

23.     return complex(c1.real-c2.real,c1.imag-c2.imag);  

24. }  

25. complex operator * (const complex &c1,const complex &c2)  

26. {  

27.     return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);  

28. }  

29. complex operator / (const complex &c1,const complex &c2)  

30. {  

31.     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));  

32. }  

33. void print(const complex &c)  

34. {  

35.     if(c.imag<0)  

36.         cout<<c.real<<c.imag<<"i";  

37.     else  

38.         cout<<c.real<<"+"<<c.imag<<"i";  

39. }  

40. int main()  

41. {  

42.     complex c1(2.0),c2(3.0,-1.0),c3;  

43.     c3=c1+c2;  

44.     cout<<"\nc1+c2= ";  

45.     print(c3);  

46.     c3=c1-c2;  

47.     cout<<"\nc1-c2= ";  

48.     print(c3);  

49.     c3=c1*c2;  

50.     cout<<"\nc1*c2= ";  

51.     print(c3);  

52.     c3=c1/c2;  

53.     cout<<"\nc1/c2= ";  

54.     print(c3);  

55.     c3=(c1+c2)*(c1-c2)*c2/c1;  

56.     cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";  

57.     print(c3);  

58.     cout<<endl;  

59.     return 0;  

60. }  

运行结果如下:

 

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

1. #include <stdio.h>  

2.   class Time  

3.   {  

4.   public:  

5.     Time(){ hours=0;minutes=0;seconds=0;} //无参构造函数  

6.     Time(int h, int m,int s) //重载构造函数  

7.     {  

8.              hours=h; minutes=m; seconds=s;  

9.     }  

10.     Time operator +(Time&); //操作符重载为成员函数,返回结果为Time类  

11.     void gettime();  

12.   private:  

13.     int hours,minutes,seconds;  

14.   };  

15.   Time Time::operator +(Time& time)  

16.   {  

17.   int h,m,s;  

18.   s=time.seconds+seconds;  

19.   m=time.minutes+minutes+s/60;  

20.   h=time.hours+hours+m/60;  

21.   Time result(h,m%60,s%60);  

22.   return result;  

23.   }  

24.   void Time::gettime()  

25.   {  

26.   printf("%d:%d:%d\n",hours,minutes,seconds);  

27.   }  

28.   int main( )  

29.   {  

30.   Time t1(9,35,45),t2(12,15,32),t3;  

31.   t3=t1+t2;  

32.   t3.gettime();  

33.   return 0;  

34.   }  

运行结果:

 

或者

1. #include <stdio.h>  

2.   class Time  

3.   {  

4.   public:  

5.     Time(){ hours=0;minutes=0;seconds=0;} //无参构造函数  

6.     Time(int h, int m,int s) //重载构造函数  

7.     {  

8.              hours=h; minutes=m; seconds=s;  

9.     }  

10.     friend Time operator +(Time&,Time&); //重载运算符为友元函数形式。  

11.                                                                  //友元不是类成员,所以这里要用两个参数,返回这两个参数的和。  

12.     void gettime( );  

13.   private:  

14.     int hours,minutes,seconds;  

15.   };  

16.      Time operator +(Time& time1,Time& time2)  

17.   {  

18.   int h,m,s;  

19.   s=time1.seconds+time2.seconds;              //计算秒数  

20.   m=time1.minutes+time2.minutes+s/60;      //计算分数  

21.   h=time1.hours+time2.hours+m/60;           //计算小时数  

22.   Time result(h,m%60,s%60);  

23.   return result;  

24.   }  

25.      void Time::gettime( )  

26.   {  

27.   printf("%d:%d:%d\n",hours,minutes,seconds);  

28.   }  

29.      int main( )  

30.   {  

31.   Time t1(9,5,24),t2(13,14,20),t3;  

32.   t3=t1+t2; //调用友元函数  

33.   t3.gettime( );  

34.      return 0;  

35.      }  

 

 

分析与讨论

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

  答:运算符函数重载一般有两种形式:重载为类的成员函数和重载为类的非成员函数。非成员函数通常是友元。(可以把一个运算符作为一个非成员、非友元函数重载;但是,这样的运算符函数访问类的私有和保护成员时,必须使用类的公有接口中提供的设置数据和读取数据的函数,调用这些函数时会降低性能。可以内联这些函数以提高性能。)  

          当运算符重载为类的成员函数时,函数的参数个数比原来的操作数要少一个(后置单目运算符除外),这是因为成员函数用this指针隐式地访问了类的一个对象,它充当了运算符函数最左边的操作数。因此:双目运算符重载为类的成员函数时,函数只显式说明一个参数,该形参是运算符的右操作数。前置单目运算符重载为类的成员函数时,不需要显式说明参数,即函数没有形参。后置单目运算符重载为类的成员函数时,函数要带有一个整型形参。

 当运算符重载为类的友元函数时,由于没有隐含的this指针,因此操作数的个数没有变化,所有的操作数都必须通过函数的形参进行传递,函数的参数与操作数自左至右一一对应。

实验总结

    通过对于本次实验的学习,我了解到了运算符函数重载的两种形式,同时从中进行了两种形式的比较:在多数情况下,将运算符重载为类的成员函数和类的友元函数都是可以的。但成员函数运算符与友元函数运算符也具有各自的一些特点:一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。以下一些双目运算符不能重载为类的友元函数:=()[]->。类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。若一个运算符的操作需要修改对象的状态,选择重载为成员函数较好。若运算符所需的操作数(尤其是第一个操作数)希望有隐式类型转换,则只能选用友元函数。当运算符函数是一个成员函数时,最左边的操作数(或者只有最左边的操作数)必须是运算符类的一个类对象(或者是对该类对象的引用)。如果左边的操作数必须是一个不同类的对象,或者是一个内部类型的对象,该运算符函数必须作为一个友元函数来实现。当需要重载运算符具有可交换性时,选择重载为友元函数。同时,我还对于函数重载有了更加深刻的认识。

实验目的和要求

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

实验内容

1.调试下列程序 

1. #include<iostream>  

2. using namespace std;  

3. class complex  

4. {  

5.     public:  

6.         complex(){real=imag=0.0;}  

7.         complex(double r){real=r;imag=0.0;}  

8.         complex(double r,double i){real=r;imag=i;}  

9.         complex operator + (const complex &c);  

10.         complex operator - (const complex &c);  

11.         complex operator * (const complex &c);  

12.         complex operator / (const complex &c);  

13.         friend void print(const complex &c);  

14.     private:  

15.         double real,imag;  

16. };  

17. inline complex complex::operator + (const complex &c)  

18. {  

19.     return complex(real+c.real,imag+c.imag);  

20. }  

21. inline complex complex::operator - (const complex &c)  

22. {  

23.     return complex(real-c.real,imag-c.imag);  

24. }  

25. inline complex complex::operator * (const complex &c)  

26. {  

27.     return complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);  

28. }  

29. inline complex complex::operator / (const complex &c)  

30. {  

31.     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));  

32. }  

33. void print(const complex &c)  

34. {  

35.     if(c.imag<0)  

36.         cout<<c.real<<c.imag<<"i";  

37.     else  

38.         cout<<c.real<<"+"<<c.imag<<"i";  

39. }  

40. int main()  

41. {  

42.     complex c1(2.0),c2(3.0,-1.0),c3;  

43.     c3=c1+c2;  

44.     cout<<"\nc1+c2= ";  

45.     print(c3);  

46.     c3=c1-c2;  

47.     cout<<"\nc1-c2= ";  

48.     print(c3);  

49.     c3=c1*c2;  

50.     cout<<"\nc1*c2= ";  

51.     print(c3);  

52.     c3=c1/c2;  

53.     cout<<"\nc1/c2= ";  

54.     print(c3);  

55.     c3=(c1+c2)*(c1-c2)*c2/c1;  

56.     cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";  

57.     print(c3);  

58.     cout<<endl;  

59.     return 0;  

60. }  

运行结果如下:

 

 

2.调试下列程序 

1. #include<iostream>  

2. using namespace std;  

3. class complex  

4. {  

5.     public:  

6.         complex(){real=imag=0.0;}  

7.         complex(double r){real=r;imag=0.0;}  

8.         complex(double r,double i){real=r;imag=i;}  

9.         friend complex operator + (const complex &c1,const complex &c2);  

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 void print(const complex &c);  

14.     private:  

15.         double real,imag;  

16. };  

17. complex operator + (const complex &c1,const complex &c2)  

18. {  

19.     return complex(c1.real+c2.real,c1.imag+c2.imag);  

20. }  

21. complex operator - (const complex &c1,const complex &c2)  

22. {  

23.     return complex(c1.real-c2.real,c1.imag-c2.imag);  

24. }  

25. complex operator * (const complex &c1,const complex &c2)  

26. {  

27.     return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);  

28. }  

29. complex operator / (const complex &c1,const complex &c2)  

30. {  

31.     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));  

32. }  

33. void print(const complex &c)  

34. {  

35.     if(c.imag<0)  

36.         cout<<c.real<<c.imag<<"i";  

37.     else  

38.         cout<<c.real<<"+"<<c.imag<<"i";  

39. }  

40. int main()  

41. {  

42.     complex c1(2.0),c2(3.0,-1.0),c3;  

43.     c3=c1+c2;  

44.     cout<<"\nc1+c2= ";  

45.     print(c3);  

46.     c3=c1-c2;  

47.     cout<<"\nc1-c2= ";  

48.     print(c3);  

49.     c3=c1*c2;  

50.     cout<<"\nc1*c2= ";  

51.     print(c3);  

52.     c3=c1/c2;  

53.     cout<<"\nc1/c2= ";  

54.     print(c3);  

55.     c3=(c1+c2)*(c1-c2)*c2/c1;  

56.     cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";  

57.     print(c3);  

58.     cout<<endl;  

59.     return 0;  

60. }  

运行结果如下:

 

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

1. #include <stdio.h>  

2.   class Time  

3.   {  

4.   public:  

5.     Time(){ hours=0;minutes=0;seconds=0;} //无参构造函数  

6.     Time(int h, int m,int s) //重载构造函数  

7.     {  

8.              hours=h; minutes=m; seconds=s;  

9.     }  

10.     Time operator +(Time&); //操作符重载为成员函数,返回结果为Time类  

11.     void gettime();  

12.   private:  

13.     int hours,minutes,seconds;  

14.   };  

15.   Time Time::operator +(Time& time)  

16.   {  

17.   int h,m,s;  

18.   s=time.seconds+seconds;  

19.   m=time.minutes+minutes+s/60;  

20.   h=time.hours+hours+m/60;  

21.   Time result(h,m%60,s%60);  

22.   return result;  

23.   }  

24.   void Time::gettime()  

25.   {  

26.   printf("%d:%d:%d\n",hours,minutes,seconds);  

27.   }  

28.   int main( )  

29.   {  

30.   Time t1(9,35,45),t2(12,15,32),t3;  

31.   t3=t1+t2;  

32.   t3.gettime();  

33.   return 0;  

34.   }  

运行结果:

 

 

1. #include <stdio.h>  

2.   class Time  

3.   {  

4.   public:  

5.     Time(){ hours=0;minutes=0;seconds=0;} //无参构造函数  

6.     Time(int h, int m,int s) //重载构造函数  

7.     {  

8.              hours=h; minutes=m; seconds=s;  

9.     }  

10.     friend Time operator +(Time&,Time&); //重载运算符为友元函数形式。  

11.                                                                  //友元不是类成员,所以这里要用两个参数,返回这两个参数的和。  

12.     void gettime( );  

13.   private:  

14.     int hours,minutes,seconds;  

15.   };  

16.      Time operator +(Time& time1,Time& time2)  

17.   {  

18.   int h,m,s;  

19.   s=time1.seconds+time2.seconds;              //计算秒数  

20.   m=time1.minutes+time2.minutes+s/60;      //计算分数  

21.   h=time1.hours+time2.hours+m/60;           //计算小时数  

22.   Time result(h,m%60,s%60);  

23.   return result;  

24.   }  

25.      void Time::gettime( )  

26.   {  

27.   printf("%d:%d:%d\n",hours,minutes,seconds);  

28.   }  

29.      int main( )  

30.   {  

31.   Time t1(9,5,24),t2(13,14,20),t3;  

32.   t3=t1+t2; //调用友元函数  

33.   t3.gettime( );  

34.      return 0;  

35.      }  

 

 

分析与讨论

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

  答:运算符函数重载一般有两种形式:重载为类的成员函数和重载为类的非成员函数。非成员函数通常是友元。(可以把一个运算符作为一个非成员、非友元函数重载;但是,这样的运算符函数访问类的私有和保护成员时,必须使用类的公有接口中提供的设置数据和读取数据的函数,调用这些函数时会降低性能。可以内联这些函数以提高性能。)  

          当运算符重载为类的成员函数时,函数的参数个数比原来的操作数要少一个(后置单目运算符除外),这是因为成员函数用this指针隐式地访问了类的一个对象,它充当了运算符函数最左边的操作数。因此:双目运算符重载为类的成员函数时,函数只显式说明一个参数,该形参是运算符的右操作数。前置单目运算符重载为类的成员函数时,不需要显式说明参数,即函数没有形参。后置单目运算符重载为类的成员函数时,函数要带有一个整型形参。

 当运算符重载为类的友元函数时,由于没有隐含的this指针,因此操作数的个数没有变化,所有的操作数都必须通过函数的形参进行传递,函数的参数与操作数自左至右一一对应。

实验总结

    通过对于本次实验的学习,我了解到了运算符函数重载的两种形式,同时从中进行了两种形式的比较:在多数情况下,将运算符重载为类的成员函数和类的友元函数都是可以的。但成员函数运算符与友元函数运算符也具有各自的一些特点:一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。以下一些双目运算符不能重载为类的友元函数:=()[]->。类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。若一个运算符的操作需要修改对象的状态,选择重载为成员函数较好。若运算符所需的操作数(尤其是第一个操作数)希望有隐式类型转换,则只能选用友元函数。当运算符函数是一个成员函数时,最左边的操作数(或者只有最左边的操作数)必须是运算符类的一个类对象(或者是对该类对象的引用)。如果左边的操作数必须是一个不同类的对象,或者是一个内部类型的对象,该运算符函数必须作为一个友元函数来实现。当需要重载运算符具有可交换性时,选择重载为友元函数。同时,我还对于函数重载有了更加深刻的认识。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值