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(){
 9             real=0;
10             imag=0;
11         }
12         Complex(double r){
13             real=r;
14             imag=0;
15         }
16         Complex(double r,double i){
17             real=r;
18             imag=i;
19         }
20         friend Complex operator+(Complex c1,Complex c2);
21         void display();
22     private:
23         double real;
24         double imag;
25 };
26 
27 Complex operator+(Complex c1,Complex c2)
28 {
29     return Complex(c1.real+c2.real,c1.imag+c2.imag);
30 }
31 
32 void Complex::display()
33 {
34     cout<<"("<<real<<","<<imag<<"i)"<<endl;
35 }
36 
37 int main(int argc, char** argv) {
38     Complex c1(3,4),c2(5,-10),c3;
39     c3=c1+2.5;
40     c3.display();
41     return 0;
42 }

 

相关文章:

  • 2021-11-20
  • 2021-11-01
  • 2022-12-23
  • 2022-01-26
  • 2021-06-17
  • 2021-04-28
  • 2022-02-07
  • 2021-12-19
猜你喜欢
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2022-01-12
相关资源
相似解决方案