复数加法

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 class Complex
 6 {
 7     public:
 8         Complex(){real=0;imag=0;}
 9         Complex(double r,double i){real=r;imag=i;}
10         Complex operator+(Complex &c2);
11         void display();
12     private:
13         double real;
14         double imag;
15 };
16 
17 Complex Complex::operator+(Complex &c2)
18 {
19     Complex c;
20     c.real=real+c2.real;
21     c.imag=imag+c2.imag;
22     return c;
23 }
24 
25 void Complex ::display()
26 {
27     cout<<"("<<real<<","<<imag<<"i)"<<endl;
28 }
29 int main(int argc, char** argv) {
30     Complex c1(3,4),c2(5,-10),c3;
31     c3=c1+c2;
32     cout<<"c1=";c1.display();
33     cout<<"c2=";c2.display();
34     cout<<"c1+c2=";c3.display();
35     return 0;
36 }

 

posted @ 2018-08-02 09:54  borter  阅读(143)  评论(0编辑  收藏  举报