转自:http://blog.csdn.net/qq_36691454/article/details/54695712?locationNum=2&fps=1
- #include <iostream>
- using namespace std;
- class complex{
- private:
- double real; //实部
- double imag; //虚部
- public:
- complex(): real(0.0), imag(0.0){ }
- complex(double a, double b): real(a), imag(b){ }
- complex operator+(const complex & A)const;
- void display()const;
- };
- //运算符重载
- complex complex::operator+(const complex & A)const{
- complex B;
- B.real = real + A.real;
- B.imag = imag + A.imag;
- return B;
- }
- void complex::display()const{
- cout<<real<<" + "<<imag<<"i"<<endl;
- }
- int main(){
- complex c1(4.3, 5.8);
- complex c2(2.4, 3.7);
- complex c3;
- c3 = c1 + c2;
- c3.display();
- return 0;
- }