有一复数类名为Complex,拥有两个doulbe类型的数据成员 real imag。
请将程序补充完整(运算符重载),能够实现一个小数加复数的运算:
例1(只有第1行为输入):
1.1 2.2 3.3
(1.1,2.2)
(0,0)
(4.4,2.2)
例2(只有第1行为输入):
-2.23 34 2
(-2.23,34)
(0,0)
(-0.23,34)
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() = default;
Complex(double real, double img)
: mReal(real),
mImg(img)
{
}
void display()
{
cout << "(" << mReal << "," << mImg << ")" << endl;
}
Complex operator+(double value)
{
return { mReal + value, mImg };
}
friend Complex operator+(double value, Complex &complex)
{
return complex + value;
}
private:
double mReal = 0.0;
double mImg = 0.0;
};
int main()
{
double a, b, d;
cin >> a >> b >> d;
Complex c1(a, b), c2;
c1.display();
c2.display();
c2 = d + c1;
c2.display();
return 0;
}