#include <iostream>
using namespace std;
class Complex
{
private:
int a;
int b;
//friend void operator<<(ostream &out, Complex &c1);
friend ostream& operator<<(ostream &out, Complex &c1);
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << "+" << b << "i" << endl;
}
};
//定义全局函数
/*
//返回空类型不支持链式编程 例如:cout << c1 << "23333" <<endl;会报错
void operator<<(ostream &out, Complex &c1)
{
out << "23333" << endl;
out<< c1.a << "+" <<c1.b << "i" <<endl;
}
*/
//实现链式编程
ostream& operator<<(ostream &out, Complex &c1)
{
out << "test" << endl;
out << c1.a << "+" <<c1.b << "i" <<endl;
return out;
}
int main()
{
Complex c1(1, 2), c2(3, 4);
cout << c1;
cout << c1 << "23333" <<endl;
return 0;
}
c++ -> 左移 && 右移 运算符重载
最新推荐文章于 2024-02-13 13:55:07 发布