欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

重载加法运算符的复数运算 代码参考

程序员文章站 2022-09-26 21:25:58
1 #include 2 3 using namespace std; 4 5 class Complex 6 { 7 private: 8 double real; 9 double image; 10 public: 11 Complex(){real=0;image=0; ......
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class complex
 6 {
 7     private:
 8         double real;
 9         double image;
10     public:
11         complex(){real=0;image=0;}
12         complex(double a){real=a;image=0;}
13         complex(double a, double b){real=a;image=b;}
14         complex operator+(complex &c)
15         {
16             complex temp;
17             temp.real=this->real+c.real;
18             temp.image=this->image+c.image;
19             return temp;
20         }
21         void show()
22         {
23             cout<<real<<"+j"<<image<<endl;
24         }
25 };
26 
27 int main()
28 {
29     double real1,real2,image1,image2;
30     cin>>real1>>image1;
31     cin>>real2>>image2;
32     complex one(real1,image1);
33     complex two(real2,image2);
34     complex three;
35     three=one+two;
36     one.show();
37     two.show();
38     three.show();
39     return 0;
40 }