<< 链式重载

时间:2025-02-14 12:36:31
 #include <iostream>

 using namespace std;

 class Complex
{
private:
int a, b;
public:
Complex(int a, int b) :a(a), b(b)
{}
friend ostream& operator<<(ostream &out, Complex &c); void operator+(Complex &c) //放在类里
{
this->a = this->a + c.a;
this->b = this->b + c.b;
//return *this;
} }; ostream& operator<<(ostream &out, Complex &c)
{
out << "a = " << c.a << " b = " << c.b;
return out;
} int main()
{
Complex c1(, ), c2(, ); cout << c1 << "showmethemoney" << "aaaaa" << c1 << endl;
//输出c1之后,返回cout(重载函数中手工返回),接着输出两个字符串(系统本身功能),再次返回cout(系统函数所定义的返回),最后又输出c1(重载函数功能),返回cout,最后endl;
system("pause");
return ;
}