#include<iostream>
using namespace std;
class Point
{
private:
int _x;
int _y;
public:
Point(int x=0,int y=0):_x(x),_y(y) {
_x = x;
_y = y;
}
void show() const;
Point& operator++(); //前置
Point& operator--(); //前置
Point operator++ (int); //后置
Point operator--(int); //后置
~Point() {};
};
Point & Point::operator++()
{
_x+=1;
_y+=1;
return Point(_x,_y);
}
Point & Point::operator--()
{
_x-=1;
_y-=1;
return Point(_x,_y);
}
Point Point::operator++(int)
{
_x = _x;
_y = _y;
return *this;// TODO: 在此处插入 return 语句
}
Point Point::operator--(int)
{
_x = _x;
_y = _y;
return *this;// TODO: 在此处插入 return 语句
}
void Point::show() const
{
cout << "点的坐标为:" <<"("<< _x << "," << _y <<")"<< endl;
cout << endl;
}
int main()
{
Point a(3, 5);
a.show();
a++.operator++(); //3,5
a.show();
a--.operator--(); //3,5
a.show();
++a.operator++(); //4,6
a.show();
--a.operator--(); //3,5
a.show();
}
对类Point重载“++”自增,“--”自减运算符,要求同时重载前缀和后缀的形式
最新推荐文章于 2023-04-05 21:27:54 发布