#include <iostream>
#include<iomanip>
using namespace std;
class Complex
{
public:
Complex(double real = 0.0, double imag = 0.0) : re(real), im(imag) {}
void printf() const
{
cout << re;
if(im>=0)
{
cout << "+" << im << "i";
}
else
{
cout << im << "i";
}
cout << endl;
}
friend Complex add(Complex cm1,Complex cm2);
private:
double re;//实部
double im; //虚部
};
Complex add(Complex cm1,Complex cm2)
{
Complex cm;
cm.re = cm1.re + cm2.re;
cm.im = cm1.im + cm2.im;
return cm;
}
int main()
{
double real1, imag1, real2, imag2;
cin >> real1 >> imag1 >> real2 >> imag2;
Complex cm1(real1,imag1);
Complex cm2(real2,imag2);
Complex result = add(cm1, cm2);
cm1.printf();
cm2.printf();
result.printf();
return 0;
}
复数 友元函数