练习74

xiaoxiao2021-02-28  68

#include <iostream> using namespace std; class Point { int _x, _y; public: Point(int x=0, int y=0) : _x(x), _y(y) {} Point& operator++(); Point operator++(int); Point& operator--(); Point operator--(int); friend ostream& operator << (ostream& o, Point& p); }; 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; } ostream& operator << (ostream& o, Point& p) { o << '(' << p._x << ", " << p._y << ')'; return o; } int main() { Point p(1, 2); cout << p << endl; cout << p++ << endl; cout << ++p << endl; cout << p-- << endl; cout << --p << endl; return 0; }
转载请注明原文地址: https://www.6miu.com/read-46562.html

最新回复(0)