#include<iostream>
using namespace std;
//重载+号(非成员,非友元),复数相加
class Complex
{
public:
Complex(int r=0,int i=0):real(r),imag(i){}
int getreal();
int getimag();
void display();
private:
int real;
int imag;
};
int Complex::getreal()
{
return real;
}
int Complex::getimag()
{
return imag;
}
void Complex::display()
{
cout << real << "+" << imag << "i" << endl;
}
Complex operator+(Complex& c1, Complex& c2)
{
return Complex(c1.getreal() + c2.getreal(), c1.getimag() + c2.getimag());
}
int main()
{
Complex c1(1, 2), c2(2, 3),c3;
c3 = c1 + c2;
c3.display();
return 0;
}
谭浩强c++第三版10-1
最新推荐文章于 2020-04-12 15:17:30 发布