實驗七

實驗目的和要求

    熟悉運算符重載的定義和使用方法。

實驗內容

1、調試下列程序。

#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.調試下列程序

#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)

程序如下:

#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(9,35,45),t2(12,15,32),t3;  
  t3=t1+t2;  
  t3.gettime();  
  return 0;  
  }

運行結果如下:

析與討論

結合上題中的程序總結運算符重載的形式。

   答:運算符函數重載一般有兩種形式:重載爲類的成員函數和重載爲類的非成員函數。非成員函數通常是友元。(可以把一個運算符作爲一個非成員、非友元函數重載;但是,這樣的運算符函數訪問類的私有和保護成員時,必須使用類的公有接口中提供的設置數據和讀取數據的函數,調用這些函數時會降低性能。可以內聯這些函數以提高性能。)  

          當運算符重載爲類的成員函數時,函數的參數個數比原來的操作數要少一個(後置單目運算符除外),這是因爲成員函數用this指針隱式地訪問了類的一個對象,它充當了運算符函數最左邊的操作數。因此:雙目運算符重載爲類的成員函數時,函數只顯式說明一個參數,該形參是運算符的右操作數。前置單目運算符重載爲類的成員函數時,不需要顯式說明參數,即函數沒有形參。後置單目運算符重載爲類的成員函數時,函數要帶有一個整型形參。

 當運算符重載爲類的友元函數時,由於沒有隱含的this指針,因此操作數的個數沒有變化,所有的操作數都必須通過函數的形參進行傳遞,函數的參數與操作數自左至右一一對應。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章