time limit: 1000ms
memory limit: 65536k
problem description
通過本題目的練習可以掌握成員運算子過載及友元運算子過載
要求定義乙個複數類,過載加法和減法運算子以適應對複數運算的要求,過載插入運算子
(<<)
以方便輸出乙個複數的要求。
input
要求在主函式中建立物件時初始化物件的值。
output
輸出資料共有
4行,分別代表a,
b的值和它們求和、求差後的值
example input
無
example output
a=3.2+4.5ib=8.9+5.6i
a+b=12.1+10.1i
a-b=-5.7-1.1i
hint
author
黃晶晶
#include using namespace std;
class num
num operator+(const num &n) const;//過載加號
num operator-(const num &n) const;//過載減號
friend ostream &operator<< (ostream &out,const num &n);//過載運算子 友元函式
private:
double x,y;
};num num::operator+(const num &n) const
num num:: operator-(const num &n) const
ostream &operator<<(ostream &out,const num &n)
int main()
sdut 4 1 複數類的運算子過載
time limit 1000ms memory limit 65536k 通過本題目的練習可以掌握成員運算子過載及友元運算子過載 要求定義乙個複數類,過載加法和減法運算子以適應對複數運算的要求,過載插入運算子 要求在主函式中建立物件時初始化物件的值。輸出資料共有4行,分別代表a,b的值和它們求和 ...
運算子過載 複數類
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 過...