实验7

实验目的和要求

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

实验内容

1.调试下列程序

  1. <span style="font-family:SimSun;font-size:16px;">//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. }</span>  
//sy7_1.cpp
#include<iostream>
using namespace std;
class complex
{
    public:
        complex(){real=imag=0.0;}
        complex(double r){real=r;imag=0.0;}
        complex(double r,double i){real=r;imag=i;}
        complex operator + (const complex &c);
        complex operator - (const complex &c);
        complex operator * (const complex &c);
        complex operator / (const complex &c);
        friend void print(const complex &c);
    private:
        double real,imag;
};
inline complex complex::operator + (const complex &c)
{
    return complex(real+c.real,imag+c.imag);
}
inline complex complex::operator - (const complex &c)
{
    return complex(real-c.real,imag-c.imag);
}
inline complex complex::operator * (const complex &c)
{
    return complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);
}
inline complex complex::operator / (const complex &c)
{
    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));
}
void print(const complex &c)
{
    if(c.imag<0)
        cout<<c.real<<c.imag<<"i";
    else
        cout<<c.real<<"+"<<c.imag<<"i";
}
int main()
{
    complex c1(2.0),c2(3.0,-1.0),c3;
    c3=c1+c2;
    cout<<"\nc1+c2= ";
    print(c3);
    c3=c1-c2;
    cout<<"\nc1-c2= ";
    print(c3);
    c3=c1*c2;
    cout<<"\nc1*c2= ";
    print(c3);
    c3=c1/c2;
    cout<<"\nc1/c2= ";
    print(c3);
    c3=(c1+c2)*(c1-c2)*c2/c1;
    cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";
    print(c3);
    cout<<endl;
    return 0;
}

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


2.调试下列程序

  1. <span style="font-family:SimSun;"><span style="font-size:16px;">//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.         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. }</span></span>  
//sy7_1.cpp
#include<iostream>
using namespace std;
class complex
{
    public:
        complex(){real=imag=0.0;}
        complex(double r){real=r;imag=0.0;}
        complex(double r,double i){real=r;imag=i;}
        friend complex operator + (const complex &c1,const complex &c2);
        friend complex operator - (const complex &c1,const complex &c2);
        friend complex operator * (const complex &c1,const complex &c2);
        friend complex operator / (const complex &c1,const complex &c2);
        friend void print(const complex &c);
    private:
        double real,imag;
};
complex operator + (const complex &c1,const complex &c2)
{
    return complex(c1.real+c2.real,c1.imag+c2.imag);
}
complex operator - (const complex &c1,const complex &c2)
{
    return complex(c1.real-c2.real,c1.imag-c2.imag);
}
complex operator * (const complex &c1,const complex &c2)
{
    return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
complex operator / (const complex &c1,const complex &c2)
{
    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));
}
void print(const complex &c)
{
    if(c.imag<0)
        cout<<c.real<<c.imag<<"i";
    else
        cout<<c.real<<"+"<<c.imag<<"i";
}
int main()
{
    complex c1(2.0),c2(3.0,-1.0),c3;
    c3=c1+c2;
    cout<<"\nc1+c2= ";
    print(c3);
    c3=c1-c2;
    cout<<"\nc1-c2= ";
    print(c3);
    c3=c1*c2;
    cout<<"\nc1*c2= ";
    print(c3);
    c3=c1/c2;
    cout<<"\nc1/c2= ";
    print(c3);
    c3=(c1+c2)*(c1-c2)*c2/c1;
    cout<<"\n(c1+c2)*(c1-c2)*c2/c1= ";
    print(c3);
    cout<<endl;
    return 0;
}

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


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

编写程序如下:

  1. <span style="font-family:SimSun;font-size:16px;"//sy7_3.cpp  
  2.     #include <stdio.h>  
  3.     class  Time  
  4.     {  
  5.     public:  
  6.       Time(){ hours=0;minutes=0;seconds=0;} //无参构造函数  
  7.       Time(int h, int m,int s)  //重载构造函数  
  8.       {  
  9.                hours=h; minutes=m; seconds=s;  
  10.       }  
  11.       Time operator +(Time&);  //操作符重载为成员函数,返回结果为Time类  
  12.       void gettime();  
  13.     private:  
  14.       int hours,minutes,seconds;  
  15.     };  
  16.     Time Time::operator +(Time& time)  
  17.     {  
  18.     int h,m,s;  
  19.     s=time.seconds+seconds;  
  20.     m=time.minutes+minutes+s/60;  
  21.     h=time.hours+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(20,30,40),t2(3,15,20),t3;  
  32.     t3=t1+t2;  
  33.     t3.gettime();  
  34.     return 0;  
  35. }</span>  
 //sy7_3.cpp
    #include <stdio.h>
    class  Time
    {
    public:
      Time(){ hours=0;minutes=0;seconds=0;} //无参构造函数
      Time(int h, int m,int s)  //重载构造函数
      {
               hours=h; minutes=m; seconds=s;
      }
      Time operator +(Time&);  //操作符重载为成员函数,返回结果为Time类
      void gettime();
    private:
      int hours,minutes,seconds;
    };
    Time Time::operator +(Time& time)
    {
    int h,m,s;
    s=time.seconds+seconds;
    m=time.minutes+minutes+s/60;
    h=time.hours+hours+m/60;
    Time result(h,m%60,s%60);
    return result;
    }
    void Time::gettime()
    {
    printf("%d:%d:%d\n",hours,minutes,seconds);
    }
 int main( )
    {
    Time t1(20,30,40),t2(3,15,20),t3;
    t3=t1+t2;
    t3.gettime();
    return 0;
}

程序输出结果如下:



分析与讨论

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

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

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

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

实验总结

 通过本次实验,我了解了运算符重载的定义,运算符重载就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。重载运算符必须遵循的规则有:重载运算符必须符合语言语法、不能重载内部C++数据类型进行操作的运算符、不能创建新的运算符、不能重载. .* :: ?:这些运算符、重载运算符要保持原有的基本语义不变。若用成员函数重载运算符需要的参数的个数总比它的操作数少一个,若用友元函数重载运算符需要的参数的个数总比它的操作数一样多。同时也多运算符重载的使用方法有了一定的了解。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值