左移运算符重载为类的友元函数:
#include<iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& cout, Person& P);
private:
int a1;
int a2;
public:
Person(int a, int b)
{
a1 = a;
a2 = b;
}
};
ostream &operator<<(ostream &cout ,Person&P)
{
cout << "a=" << P.a1 << "b=" << P.a2;
return cout;
}
void dis()
{
Person P(10,10);
cout << P << endl;
}
int main()
{
dis();
return 0;
}