用c++定义Point类,有坐标x,y两个成员变量;对Point类重载“++”、“- -”运算符,main实现对坐标值的改变

2024-12-02 03:57:07
推荐回答(1个)
回答1:

#include

class Point
{
private:
int x;
int y;
public:
Point (int a,int b){x=a;y=b;}
Point &operator ++(int); //后置++
Point &operator --(int); //后置--
Point &operator ++(); //前置++
Point &operator --(); //前置--
void print();
};

Point &Point::operator++(int)
{return Point(x++,y++);}

Point &Point::operator--(int)
{return Point(x--,y--);}

Point &Point::operator++()
{return Point(++x,++y);}

Point &Point::operator--()
{return Point(--x,--y);}

void Point::print()
{cout<<"x="<
main()
{ Point a1(2,3);
a1++;
a1.print();
}

好了吧