首页 > 编程语言 > 详细

C++ 运算符重载<<

时间:2014-03-27 01:53:33      阅读:516      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 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 }
bubuko.com,布布扣

C++ 运算符重载<<,布布扣,bubuko.com

C++ 运算符重载<<

原文:http://www.cnblogs.com/autumoonchina/p/3627119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!