码迷,mamicode.com
首页 > 其他好文 > 详细

<< 链式重载

时间:2016-02-06 18:23:11      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Complex
 6 {
 7 private:
 8     int a, b;
 9 public:
10     Complex(int a, int b) :a(a), b(b)
11     {}
12     friend ostream& operator<<(ostream &out, Complex &c);
13 
14     void operator+(Complex &c)          //放在类里
15     {
16           this->a = this->a + c.a;
17           this->b = this->b + c.b;
18           //return *this;
19     }
20 
21 };
22 
23 ostream& operator<<(ostream &out, Complex &c)
24 {
25     out << "a = " << c.a << "  b = " << c.b;
26     return out;
27 }
28 
29 int main()
30 {
31     Complex c1(1, 2), c2(3, 4);
32 
33     cout << c1 << "showmethemoney" << "aaaaa" << c1 << endl;
34     //输出c1之后,返回cout(重载函数中手工返回),接着输出两个字符串(系统本身功能),再次返回cout(系统函数所定义的返回),最后又输出c1(重载函数功能),返回cout,最后endl;
35     system("pause");
36     return 0;
37 }

 

<< 链式重载

标签:

原文地址:http://www.cnblogs.com/c-slmax/p/5184128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!