class Complex
{
public:
Complex(double r=0, double i=0):real(r), imag(i){ }
Complex operator+( ?? ) const;//多載雙目運算子'+'
Complex operator-=( ?? ); //多載雙目運算子'-='
friend Complex operator-( ?? ) const;//多載雙目運算子'-'
void Display() const;
private:
double real;
double imag;
};
void Complex::Display() const
{
cout << "(" << real << ", " << imag << ")" << endl;
}
int main()
{
double r, m;
cin >> r >> m;
Complex c1(r, m);
cin >> r >> m;
Complex c2(r, m);
Complex c3 = c1+c2;
c3.Display();
c3 = c1-c2;
c3.Display();
c3 -= c1;
c3.Display();
return 0;
}
輸入格式:
輸入有兩行,分別為兩個復數的實部與虛部。
輸出格式:
按樣例格式輸出結果。
輸入樣例:
在這里給出一組輸入。例如:
4 2
3 -5
輸出樣例:
在這里給出相應的輸出。例如:
(7, -3)
(1, 7)
(-3, 5)
uj5u.com熱心網友回復:
供參考:#include <iostream.h>
class Complex
{
public:
Complex(double r=0, double i=0):real(r), imag(i){}
Complex operator +(const Complex &) const;//多載雙目運算子'+'
Complex operator-=(const Complex &); //多載雙目運算子'-='
friend Complex operator-(const Complex &,const Complex &);//多載雙目運算子'-'
void Display() const;
private:
double real;
double imag;
};
Complex Complex::operator+(const Complex &c)const
{
return Complex(real+c.real,imag+c.imag);
}
Complex Complex::operator-=(const Complex &c)
{
//real -= c.real;
//imag -= c.imag;
return Complex(real-=c.real,imag-=c.imag);
}
void Complex::Display() const
{
cout << "(" << real << ", " << imag << ")" << endl;
}
Complex operator-(const Complex &a,const Complex &b)
{
return Complex(a.real - b.real, a.imag - b.imag);
}
int main()
{
double r, m;
cin >> r >> m;
Complex c1(r, m);
cin >> r >> m;
Complex c2(r, m);
Complex c3 = c1+c2;
c3.Display();
c3 = c1-c2;
c3.Display();
c3 -= c1;
c3.Display();
return 0;
}
//4 2
//3 -5
//(7, -3)
//(1, 7)
//(-3, 5)
//請按任意鍵繼續. . .
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/270147.html
標籤:C++ 語言
上一篇:畢業設計車牌識別原始碼
下一篇:菜鳥安裝codeblocks
