carysu@126.com

carysu@126.com
posts - 11, comments - 0, trackbacks - 0, articles - 0

运算符重载

Posted on 2011-07-16 19:36 susu 阅读(202) 评论(0)  编辑 收藏 引用
 1#include <iostream>
 2#include <string>
 3using namespace std;
 4
 5
 6class 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}
;
35class 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}
;
43complex summator :: addition(complex a,complex b)
44{
45    return a + b;
46}

47
48void 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

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理