65,210
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Point
{
friend ostream& operator<<(ostream& out,const Point& rhs);
friend Point operator+(const Point& lhs,const Point& rhs);
public:
Point(int x = 0,int y = 0);
public:
Point& operator++();
private:
int m_x;
int m_y;
};
ostream& operator<<(ostream& out,const Point& rhs)
{
out << "(" << rhs.m_x << "," << rhs.m_y << ")";
return out;
}
Point operator+(const Point& lhs,const Point& rhs)
{
Point result(lhs);
result.m_x += rhs.m_x;
result.m_y += rhs.m_y;
return result;
}
Point::Point(int x, int y)
{
m_x = x;
m_y = y;
}
Point& Point::operator ++()
{
++this->m_x;
++this->m_y;
return *this;
}
int main()
{
Point p1(3,4),p2(5,6),p3;
p3=p1+p2;
cout <<p1 <<"+" <<p2 <<"=" <<p3 <<endl;
//期望输出:(3,4)+(5,6)=(8,10)
++p3 ;
cout <<p3 <<endl ; // 期望输出:(9,11)
return 0 ;
}