0
点赞
收藏
分享

微信扫一扫

流插入运算符的重载

司马吹风 2022-02-27 阅读 68

基础模板:

ostream& ostream::operator<<(int n)
{
	//...输出n的代码
	return *this;
}
ostream& ostream::operator<<(const char* s)
{
	//输出s的代码
	return *this;
}

升级模板:

#include<iostream>
using namespace std;
class CStudent
{
	public:
		int nAge;	
		friend ostream& operator<<(ostream& a,const CStudent& b);
};
ostream& operator<<(ostream& a,const CStudent& b)
		{
			a<<b.nAge;
			return a;
		}
int main()
{
	CStudent a;
	a.nAge=5;
	cout<<a<<"HEllo";
	return 0;
}
举报

相关推荐

0 条评论