正文
对类Point重载"++"(自增)、"–"(自减)运算符,要求同时重载前缀和后缀的形式。
#include <iostream> using namespace std; // Point类 class Point { // 构造函数与析构函数 public: Point(int _x = 0, int _y = 0) : x(_x), y(_y){} ~Point(){} // 普通成员函数 public: // 输出点的坐标 void showPosition(){cout << "(" << x << ", " << y << ")" << endl;} // 运算符函数重载 public: // 前置, ++i\--i Point operator ++(); Point operator --(); // 后置,i++\i-- Point operator ++(int); Point operator --(int); private: int x; int y; }; Point Point::operator ++() { Point po; po.x = x + 1; po.y = y + 1; x += 1; y += 1; return po; } Point Point::operator --() { Point po; po.x = x - 1; po.y = y - 1; x -= 1; y -= 1; return po; } Point Point::operator ++(int) { Point po; po.x = x; po.y = y; x += 1; y += 1; return po; } Point Point::operator --(int) { Point po; po.x = x; po.y = y; x -= 1; y -= 1; return po; } int main() { Point po(0, 0); Point po1(2, 3); Point po2(1, 4); // 三点初始坐标 cout << "Position po: "; po.showPosition(); cout << "Position po1: "; po1.showPosition(); cout << "Position po2: "; po2.showPosition(); cout << "_______________" << endl; // 后置运算后的坐标 po = po1++; cout << "Pos ++: po = "; po.showPosition(); cout << "Pos ++: po1 = "; po1.showPosition(); po = po2--; cout << "Pos --: po = "; po.showPosition(); cout << "Pos --: po2 = "; po2.showPosition(); cout << "_______________" << endl; // 前置运算后的坐标 po = ++po1; cout << "Pre ++: po = "; po.showPosition(); cout << "Pre ++: po1 = "; po1.showPosition(); po = --po2; cout << "Pre --: po = "; po.showPosition(); cout << "Pre --: po2 = "; po2.showPosition(); return 0; }
输出
Position po: (0, 0) Position po1: (2, 3) Position po2: (1, 4) _______________ # po = po1++; Pos ++: po = (2, 3) Pos ++: po1 = (3, 4) # po = po2--; Pos --: po = (1, 4) Pos --: po2 = (0, 3) _______________ # po = ++po1; Pre ++: po = (4, 5) Pre ++: po1 = (4, 5) # po = --po2; Pre --: po = (-1, 2) Pre --: po2 = (-1, 2)