第八周 專案1 用友元函式實現複數類中的運算子過載

2021-07-02 02:17:42 字數 775 閱讀 8966

/*

*檔名稱:time.cpp

*版本號:v1.0

*問題描述:請用類的友元函式,定義複數類過載運算子+、-、*、/,使之能用於複數的加減乘除.

*輸入描述:無

*程式輸出:無

*/#includeusing namespace std;

class complex

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;

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

void complex::display()

{ cout<

執行結果:

學習心得:

友元函式並不是類的成員函式,不必用域運算子呼叫。注意形參是兩個。

第八周專案1

include include sqstring.h int main cpp view plain copy sqstring include include include sqstring.h void strassign sqstring s,char cstr s為引用型引數 void s...

第八周 專案一(1)

檔名稱 test.cpp 完成日期 2016年4月19日 版本號 v1.0 問題描述 實現salary類中的成員函式,在main函式定義salary類的物件,輸入工資,再給每個人漲500元工資,排序後工資資料,然後輸出結果。include using namespace std class sala...

第八周專案1 2利用友元函式實現運算子過載

作 者 石堯 完成日期 2014 年04 月 15日 版 本 號 v1.0 問題描述 利用友元函式實現運算子過載。樣例輸入 略.樣例輸出 略。問題分析 略。includeusing namespace std class complex complex double r,double i compl...