包含转换重载的程序

 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 }

 

posted @ 2018-08-02 10:04  borter  阅读(104)  评论(0编辑  收藏  举报