#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() { };
// 在此处补充你的代码
friend ostream & operator<< (ostream & o, Point & p){
cout<<p.x<<","<<p.y;
return o;
}
friend int operator >>(istream & i,Point & p){
cin>>p.x>>p.y;
return 1;
}
};
int main()
{
Point p;
while(cin >> p) {//关键是重载>>和<<
cout << p << endl;
}
system("pause");
return 0;
}
mooc 016openjudge 惊呆!Point竟能这样输入输出 对<<和>>的重载
最新推荐文章于 2024-12-28 23:00:46 发布
这篇博客介绍了如何在C++中为自定义类`Point`重载`<<`和`>>`操作符,以便实现友元函数进行对象的输入和输出。示例代码展示了一个简单的点坐标类,并在`main`函数中使用循环读取并打印点坐标,直到输入结束。
573

被折叠的 条评论
为什么被折叠?



