33 lines
504 B
C++
33 lines
504 B
C++
// 重载 C++ 的 << 输出流运算符
|
|
#include <iostream>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
class A
|
|
{
|
|
friend ostream &operator<<(ostream &cout, A &a);
|
|
|
|
private:
|
|
int x, y;
|
|
|
|
public:
|
|
A(int x, int y) : x(x), y(y) {}
|
|
};
|
|
|
|
// << 输出运算符重载
|
|
ostream &operator<<(ostream &cout, A &a)
|
|
{
|
|
// x, y 是 A 类的私有成员
|
|
cout << "a.x : " << a.x << ", a.y : " << a.y << endl;
|
|
return cout;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
A a1(1, 2);
|
|
cout << a1;
|
|
return 0;
|
|
}
|