左移运算符重载

#include
using namespace std;

//左移运算符重载  作用:输出自定义类型

class Person
{
	friend ostream& operator<<(ostream& cout, Person& p);	//	友元
private:
	int m_A;
	int m_B;

public:
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
};

//只能使用全局函数重载左移运算符
ostream& operator<<( ostream& cout,Person& p)
{
	cout << "p.m_A = " << p.m_A << " " << "p.m_B = " << p.m_B ;
	return cout;
}

//测试函数
void test()
{
	Person p(10,10);
	cout << p << endl;
}

//主函数
int main()
{
	test();
	
	system("pause");
	return 0;
}

 

你可能感兴趣的:(左移运算符重载)