通過對運算子過載進行複數簡單運算

2021-09-22 12:48:49 字數 2349 閱讀 6388

對複數的除法運算有瑕疵;如何能在不更改show()函式的情況下使得複數輸出能保留位數輸出。。。。。

#include #include using namespace std;

class complex

double real()

double imag()

complex operator+(const complex &c);

complex operator+(const double &d)

complex operator=(const complex &c);

complex operator-(const complex &c);

complex operator-(const double &d)

complex operator*(const complex &c);

complex operator*(const double &d)

complex operator/(const double &d)

complex operator/(const complex &c);

void show();

};/*

//該法會改變第乙個資料的資料,如c3=c2+c1;用此加法會使c2與c3相等(均是計算後的資料)

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

*///該法不會改變第乙個資料的資料

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

/* //用該法定義賦值行為將不會改變*this的資料

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

*/ complex complex::operator=(const complex &c)

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

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

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

void complex::show()

#include #include using namespace std;

class complex

double real()

double imag()

complex operator+(const complex &c);

complex operator+(const double &d)

complex operator=(const complex &c);

complex operator-(const complex &c);

complex operator-(const double &d)

complex operator*(const complex &c);

complex operator*(const double &d)

complex operator/(const double &d)

complex operator/(const complex &c);

void show();

};/*

//該法會改變第乙個資料的資料,如c3=c2+c1;用此加法會使c2與c3相等(均是計算後的資料)

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

*///該法不會改變第乙個資料的資料

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

/* //用該法定義賦值行為將不會改變*this的資料

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

*/ complex complex::operator=(const complex &c)

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

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

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

void complex::show()

c2.show();

cout<<")=(";

c.show();

cout<<")"

return 0;

}

用成員運算子過載函式進行複數運算

includeusing namespace std class complex 宣告複數類complex complex complex double r,double i 定義建構函式 complex complex operator complex c 過載運算子 的實現 complex co...

運算子過載 複數類

1.普通運算子可以過載 2.型別過載1 防止實參被修改 2 接收隱式生成臨時物件 類內是this call的呼叫約定,成員方法隱藏this指標,指向一般為左運算元,所以只用傳右運算元即可 如下 class ccomplex 構造類內實現 的過載 右運算元型別為intconst ccomplex op...

複數類運算子過載

includeusing namespace std class complex complex operator complex co 過載 號,實現複數相加 complex operator complex co 過載 號,實現複數相減 complex operator complex co 過...