C 實踐參考 複數類中的運算子過載

2021-09-23 17:43:39 字數 4517 閱讀 7536

返回:賀老師課程教學鏈結

【專案-實現複數類中的運算子過載】

(1)請用類的成員函式,定義複數類過載運算子+、-、*、/,使之能用於複數的加減乘除

class complex 

complex(double r,double i)

complex operator+(const complex &c2);

complex operator-(const complex &c2);

complex operator*(const complex &c2);

complex operator/(const complex &c2);

void display();

private:

double real;

double imag;

};//下面定義成員函式

//下面定義用於測試的main()函式

int main()

[參考解答]

#include using namespace std;

class complex

complex(double r,double i)

complex operator+(const complex &c2);

complex operator-(const complex &c2);

complex operator*(const complex &c2);

complex operator/(const complex &c2);

void display();

private:

double real;

double imag;

};//下面定義成員函式

//複數相加: (a+bi)+(c+di)=(a+c)+(b+d)i.

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

//複數相減:(a+bi)-(c+di)=(a-c)+(b-d)i.

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

//複數相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

complex complex::operator*(const complex &c2)

//複數相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

complex complex::operator/(const complex &c2)

void complex::display()

complex(double r,double i)

friend complex operator+(complex &c1, complex &c2);

friend complex operator-(complex &c1, complex &c2);

friend complex operator*(complex &c1, complex &c2);

friend complex operator/(complex &c1, complex &c2);

void display();

private:

double real;

double imag;

};//複數相加:(a+bi)+(c+di)=(a+c)+(b+d)i.

complex operator+(complex &c1, complex &c2)

//複數相減:(a+bi)-(c+di)=(a-c)+(b-d)i.

complex operator-(complex &c1, complex &c2)

//複數相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

complex operator*(complex &c1, complex &c2)

//複數相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

complex operator/(complex &c1, complex &c2)

void complex::display()

complex(double r,double i)

double getreal() const

double getimag() const

void setreal(double r)

void setimag(double i)

void display();

private:

double real;

double imag;

};//複數相加:(a+bi)+(c+di)=(a+c)+(b+d)i.

complex operator+(const complex &c1, const complex &c2) //(3)將引數處理為const更符合需求

//複數相減:(a+bi)-(c+di)=(a-c)+(b-d)i.

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

//複數相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

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

//複數相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

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

void complex::display()

complex(double r,double i)

friend complex operator+(complex &c1, complex &c2);

friend complex operator+(double d1, complex &c2);

friend complex operator+(complex &c1, double d2);

friend complex operator-(complex &c1, complex &c2);

friend complex operator-(double d1, complex &c2);

friend complex operator-(complex &c1, double d2);

friend complex operator*(complex &c1, complex &c2);

friend complex operator*(double d1, complex &c2);

friend complex operator*(complex &c1, double d2);

friend complex operator/(complex &c1, complex &c2);

friend complex operator/(double d1, complex &c2);

friend complex operator/(complex &c1, double d2);

void display();

private:

double real;

double imag;

};//複數相加:(a+bi)+(c+di)=(a+c)+(b+d)i.

complex operator+(complex &c1, complex &c2)

complex operator+(double d1, complex &c2)

complex operator+(complex &c1, double d2)

//複數相減:(a+bi)-(c+di)=(a-c)+(b-d)i.

complex operator-(complex &c1, complex &c2)

complex operator-(double d1, complex &c2)

complex operator-(complex &c1, double d2)

//複數相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

complex operator*(complex &c1, complex &c2)

complex operator*(double d1, complex &c2)

complex operator*(complex &c1, double d2)

//複數相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

complex operator/(complex &c1, complex &c2)

complex operator/(double d1, complex &c2)

complex operator/(complex &c1, double d2)

void complex::display()

int main()

C 實踐參考 Time類中的運算子過載

專案 time類中的運算子過載 實現time類中的運算子過載。class ctime 提示1 並不是所有比較運算過載函式都很複雜 比較運算返回的是比較結果,是bool型的true或false 可以直接使用已經過載了的運算實現新運算,例如果已經實現了 則實現 就可以很方便了 bool ctime op...

C 實踐參考 Time類中的運算子過載

all right reserved.檔名稱 test.cpp 作 者 韓雙志 完成日期 2016年5月24日 版本號 v1.0 問題描述 實現time類中的運算子過載 輸入描述 輸入兩個時間格式為時 分 秒 輸出描述 輸出比較後的時間 include using namespace std cla...

C 複數類運算子過載

實現運算子過載有兩種形式 過載為成員函式 過載為友元函式。includeusing namespace std class complex complex double r,double i complex const complex rhs complex operator const compl...