cout格式比较固定,要记住怎么用:
#include<iostream>
using namespace std;
class stu
{
private:
int id;
string name;
public:
friend ostream& operator<<(ostream &out,stu s);
void set()
{
int i;
cout<<"id=";cin>>i;
id=i;
string n;
cout<<"name=";cin>>n;
name=n;
}
};
ostream& operator<<(ostream &out,stu s)
{
cout<<s.name<<"-------"<<s.id;
return out;
}
int main()
{
stu A;
A.set();
cout<<A<<endl;
}
输出结果为:
输入:id=123
name=aaa
输出:aaa-------123