class Point private. int_xy public: Point & operator- - ();Point operator- -int); Please finish the definition of the operator overloading Point& Point:;operator--0 Point Point::opcrator- -(int)
时间: 2023-08-02 10:34:34 浏览: 119
Here is the completed definition of the operator overloading functions:
```c++
class Point {
private:
int _x;
int _y;
public:
Point(int x = 0, int y = 0) : _x(x), _y(y) {}
// Prefix decrement operator overloading
Point& operator--() {
_x--;
_y--;
return *this;
}
// Postfix decrement operator overloading
Point operator--(int) {
Point temp = *this;
_x--;
_y--;
return temp;
}
// Friend function to output Point objects
friend std::ostream& operator<<(std::ostream& os, const Point& p) {
os << "(" << p._x << ", " << p._y << ")";
return os;
}
};
int main() {
Point p(5, 7);
std::cout << "p = " << p << std::endl;
// Prefix decrement
--p;
std::cout << "--p = " << p << std::endl;
// Postfix decrement
Point q = p--;
std::cout << "q = " << q << std::endl;
std::cout << "p = " << p << std::endl;
return 0;
}
```
In this example, we overload both the prefix decrement operator (`--`) and the postfix decrement operator (`--int`). The prefix decrement operator returns a reference to the Point object after decrementing its `_x` and `_y` members. The postfix decrement operator returns a copy of the original Point object before decrementing its members.
We also define a friend function `operator<<` to output Point objects in a nice format. Using these operator overloading functions, we can easily manipulate Point objects using the decrement operators.
阅读全文