根據以下代碼段完善 ?? 處內容及程序內容,以實現規定的輸出。
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; }
1 #include<iostream> 2 using namespace std; 3 4 class Complex 5 { 6 public: 7 Complex(double r=0,double i=0):real(r), imag(i){} 8 Complex operator+(Complex &) const;//重載雙目運算符'+' 9 Complex operator-=(Complex &); //重載雙目運算符'-=' 10 friend const Complex operator-(Complex &,Complex &);//重載雙目運算符'-' 11 void Display() const; 12 private: 13 double real; 14 double imag; 15 }; 16 17 Complex Complex::operator + (Complex &C) const 18 { 19 return Complex(real+C.real,imag+C.imag); //重載 + 返回值為相加之后的結果 20 } 21 22 Complex Complex::operator-=(Complex &C) 23 { 24 real=real-C.real; 25 imag=imag-C.imag; 26 return Complex(real,imag); //重載 -= 返回值為減后結果,且對象本身數值也發生變化 27 } 28 29 Complex const operator - (Complex &C1,Complex &C2) //重載 - 返回值為相減后的結果 30 { 31 return Complex(C1.real-C2.real,C1.imag-C2.imag); 32 } 33 34 void Complex::Display() const 35 { 36 cout << "(" << real << ", " << imag << ")" << endl; 37 } 38 39 int main() 40 { 41 double r, m; 42 cin >> r >> m; 43 Complex c1(r, m); 44 cin >> r >> m; 45 Complex c2(r, m); 46 Complex c3 = c1+c2; 47 c3.Display(); 48 c3 = c1-c2; 49 c3.Display(); 50 c3 -= c1; 51 c3.Display(); 52 return 0; 53 }