C++ 运算符重载<<

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Good
 5 {
 6 public:
 7     int member;
 8     Good(int a): member(a)
 9     {
10         cout << "调用构造" << endl;
11     }
12     void show()
13     {
14         cout << member << endl;
15     }
16     Good operator+(const Good& obj)
17     {
18         return Good(member - obj.member);
19     }
20     
21     friend ostream& operator<<(ostream& os, Good& obj)
22     {
23         os << obj.member << "运算符重载" << endl;
24         
25         return os; //这句话关键,便于重复调用 
26     }
27 };
28 
29 int main(int argc, char *argv[])
30 {
31     Good a(10), b(20);
32     Good c = a + b;
33     cout << c;
34     c.show();
35     return 0;
36 }

 

posted @ 2014-03-26 22:01  秋月的私语  阅读(213)  评论(0编辑  收藏  举报