自考新教材-p173_3(2)

源程序:

#include <iostream>
using namespace std;

class myComplex
{
public:
int real, imag;
myComplex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
};
myComplex operator+(myComplex &a, myComplex &b)
{
int r = a.real + b.real;
int i = a.imag + b.imag;
return myComplex(r,i);
}

int main()
{
myComplex x(1, 2), y(3, 4), z;
z = x + y;
cout << z.real << "+" << z.imag << "i" << endl;
system("pause");
return 1;
}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12275512.html