根据以下代码段完善 ?? 处内容及程序内容,以实现规定的输出。
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 }