对类Point重载++,–运算符,要求同时重载前缀和后缀的形式
#include
using namespace std;
class Point {
public:
Point& operator++();
Point operator++(int);
Point& operator–();
Point operator–(int);
Point() { x = y = 0; }
int X() { return x; }
int Y() { return y; }
private:
int x, y;
};
Point & Point::operator++() {
x++;
y++;
return *this;
}
Point Point::operator++(int) {
Point temp = *this;
++(*this);
return temp;
}
Point & Point::operator–() {
x–;
y–;
return *this;
}
Point Point::operator–(int) {
Point temp = *this;
–(*this);
return temp;
}
void main() {
Point A ;
cout << “A的值为:” << A.X() << “,” << A.Y() << endl;
A++;
cout << “A的值为:” << A.X() << “,” << A.Y() << endl;
++A;
cout << “A的值为:” << A.X() << “,” << A.Y() << endl;
A–;
cout << “A的值为:” << A.X() << “,” << A.Y() << endl;
–A;
cout << “A的值为:” << A.X() << “,” << A.Y() << endl;
}
对类Point 重载++,--运算符
最新推荐文章于 2020-12-13 15:12:15 发布