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 }