天天看點

複數類的操作符重載

複數類重載主要實作二進制操作符(參數中有一個該類的對象)‘+’、‘-’和一進制操作符(參數中沒有該類的對象)、‘-a’(取負)。

代碼:

#include <iostream>

using namespace std ;

class complex

{

 public:

 complex() {

 }

 complex(int r, int i) {real=r; p_w_picpath=i;}

 complex(const complex&) ;

 complex operator +(const complex&);

 complex operator -(const complex&);

 complex operator -();

 void print();

 private:

 int real, p_w_picpath;

};

complex::complex(const complex &c)

 real = c.real;

 p_w_picpath = c.p_w_picpath;

}

complex complex::operator +(const complex &c )

 real += c.real;

 p_w_picpath += c.p_w_picpath;

 return *this;

complex complex::operator -(const complex &c )

 complex temp;

 temp.real = real - c.real;

 temp.p_w_picpath= p_w_picpath - c.p_w_picpath ;

 return temp;

繼續閱讀