016:惊呆!Point竟然能这样输入输出
题面
描述
程序填空
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() { };
// 在此处补充你的代码
};
int main()
{
Point p;
while(cin >> p) {
cout << p << endl;
}
return 0;
}
思路
观察发现明显要重载>>,<<
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() { };
friend istream& operator >> (istream& is,Point& x) {
is >> x.x >> x.y;
return is;
}
friend ostream& operator << (ostream& os, const Point & x) {
os << x.x << "," << x.y;
return os;
}
};
int main()
{
Point p;
while (cin >> p) {
cout << p << endl;
}
return 0;
}