运算符重载

 1 #include  < iostream >
 2 #include  < string >
 3 using   namespace  std;
 4
 5
 6 class  complex
 7 {
 8    double a;
 9    double b;
10public:
11    complex()
12    {
13        a = 0;
14        b = 0;
15    }

16    complex operator + (complex another)
17    {
18        complex add;
19        add.a = a + another.a;
20        add.b = b + another.b;
21        return add;
22    }

23    void Putln()
24    {
25        cin >> a >> b;
26        return;
27    }

28    void show()
29    {
30        //输出复数
31        cout <<"("<<a<<"+"<<b<<"i)";
32        return;
33    }

34}
;
35 class  summator
36 {
37public:
38    int addition(int a,int b);
39    float addition(float a,float b);
40    string addition(string a,string b);
41    complex addition(complex a,complex b);
42}
;
43 complex summator :: addition(complex a,complex b)
44 {
45    return a + b;
46}

47
48 void  main()
49 {
50    summator sr;
51    //int inta ,intb;
52    //float fa ,fb;
53    //string stra,strb;
54    complex ca,cb;
55    
56    
57    cout << "请输入两个复数:"<<endl;
58    ca.Putln();
59    cb.Putln();
60
61    ca.show();
62    cout<< "+";
63    cb.show();
64    cout<< "=";
65
66    sr.addition(ca,cb).show();
67    cout<<endl;
68
69    return;
70}

71

你可能感兴趣的:(运算符重载)