重载运算符

重载运算符“<<”和“>>”

 

程序代码:

#include <iostream.h>
class Point 
{
public:
	Point (){};
	friend istream & operator >> (istream &in,Point &p); 
	friend ostream & operator << (ostream &out,Point &p); 
private:
	int x,y;
};

istream & operator >> (istream &in,Point &p)
{
	cout << "请输入x、y的值: "<<endl;
	cout << "x= ";
	in >> p.x;
	cout << "y= ";
	in >> p.y;
	return in;
}

ostream & operator << (ostream &out,Point &p)
{
	out <<"输出x、y的值:"<<endl;
	out <<"x="<<p.x<<"   y="<<p.y<<endl;
	return out;
}

void main()
{
	Point P;
	cin>>P;
	cout<<P;
}

 

运行结果:



 

你可能感兴趣的:(C++,程序设计,重载运算符)