输出运算符重载格式
ostream & operator<<(ostream &out,person a)
{
cout << a.a << endl;
return out;
}
举例输出运算符重载
#include "iostream"
using namespace std;
class person
{
public:
person(int a):a(a){}
int a;
};
ostream & operator<<(ostream &out,person a)
{
cout << a.a << endl;
return out;
}
int main()
{
person a(10);
person b(99);
cout << a << b ;
}
输入运算符重载格式
istream & operator>>(istream &in,person &a)
{
cin>>a.a;
return in;
}
举例输入运算符重载
#include "iostream"
using namespace std;
class person
{
public:
person(int a):a(a){}
int a;
};
ostream & operator<<(ostream &out,person a)
{
cout << a.a << endl;
return out;
}
istream & operator>>(istream &in,person &a)
{
cin>>a.a;
return in;
}
int main()
{
person a(10);
person b(99);
cout << a << b;
cin>>a>>b;
cout << a << b;
}