使用運算子過載實現複數的四則運算

2021-06-22 10:17:22 字數 862 閱讀 7259

程式**:

#include using namespace std;

class complex

//使用初始化表初始化複數

complex(double r, double i):real(r),imag(i){}

complex operator+(complex &c2);//複數的加法

complex operator-(complex &c2);//複數的減法

complex operator*(complex &c2);//複數的乘法

complex operator/(complex &c2);//複數的除法

void display( );//顯示複數

private:

double real;//複數的實部

double imag;//複數的虛部

};//複數的加法

complex complex::operator+(complex &c2)

//複數的減法

complex complex::operator-(complex &c2)

//複數的乘法

complex complex::operator*(complex &c2)

//複數的除法

complex complex::operator/(complex &c2)

//顯示複數

void complex::display( )

{ cout<

執行結果:

C 四則運算符與關係運算子的過載

include include include include using namespace std class couple couple operator const couple c couple operator const int k couple operator const coup...

給出複數,實現複數的四則運算

1.定義兩個類 乙個為complex,另外乙個為complex oper。計算功能在complex類中實現,方法分別為 public staticcomplex add complex t1,complex t2 public staticcomplex sub complex t1,complex...

js四則運算符

只有當加法運算時,其中一方是字串型別,就會把另乙個也轉為字串型別。其他運算只要其中一方是數字,那麼另一方就轉為數字。並且加法運算會觸發三種型別轉換 將值轉換為原始值,轉換為數字,轉換為字串。template section class p 10 el button type danger click...