#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, const Point& p); }; /********** Begin **********/ /********** End **********/ ostream& operator << (ostream& o, const Point& p) { o << '(' << p._x << ", " << p._y << ')'; return o; } int main() { int x,y; cin>>x>>y; Point p(x, y); cout << p << endl; cout << p++ << endl; cout << ++p << endl; cout << p-- << endl; cout << --p << endl; return 0; }
时间: 2023-08-06 09:06:58 浏览: 91
以下是完整的C++代码实现:
```c++
#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, const Point& p);
};
Point& Point::operator++() {
_x++;
_y++;
return *this;
}
Point Point::operator++(int) {
Point tmp(*this);
_x++;
_y++;
return tmp;
}
Point& Point::operator--() {
_x--;
_y--;
return *this;
}
Point Point::operator--(int) {
Point tmp(*this);
_x--;
_y--;
return tmp;
}
ostream& operator << (ostream& o, const Point& p) {
o << '(' << p._x << ", " << p._y << ')';
return o;
}
int main() {
int x, y;
cin >> x >> y;
Point p(x, y);
cout << p << endl;
cout << p++ << endl;
cout << ++p << endl;
cout << p-- << endl;
cout << --p << endl;
return 0;
}
```
输入示例:
```
1 2
```
输出示例:
```
(1, 2)
(1, 2)
(3, 4)
(3, 4)
(1, 2)
```
阅读全文