point&operator ++和point operator ++(int)
时间: 2023-08-16 14:06:12 浏览: 62
这两个函数都是重载了运算符 "++" 的类成员函数,不同的是它们的参数不同,前者是一个引用类型的左值,后者是一个 int 类型的右值。
point& operator ++ 表示前置自增运算符,它的返回值为 point 类型的引用,用于支持链式调用。该函数将对象本身加1后返回自身的引用,比如 ++p。
point operator ++(int) 表示后置自增运算符,它的参数 int 实际上是一个占位符,用于区分前置自增运算符。该函数返回一个 point 类型的临时对象,表示在原对象值的基础上加1,比如 p++。注意,由于返回的是临时对象,因此它不能被修改,否则会出现未定义的行为。
相关问题
声明 Point 类,有坐标 _ x,_ y 两个成员变量;对Point 类重载“++”(自增)、“--”(自减)运算符,实现对坐标值的改变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 对象,并进行自增自减操作。以下是完整的代码实现:
```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 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, const Point& p) {
o << "(" << p._x << ", " << p._y << ")";
return o;
}
int main() {
Point p1(1, 1);
cout << "p1: " << p1 << endl;
++p1;
cout << "p1 after prefix ++: " << p1 << endl;
p1++;
cout << "p1 after postfix ++: " << p1 << endl;
--p1;
cout << "p1 after prefix --: " << p1 << endl;
p1--;
cout << "p1 after postfix --: " << p1 << endl;
return 0;
}
```
以上代码演示了对 Point 类进行自增自减操作,并输出结果。
编写程序声明Point类,在类中声明整型的私有成员变量x、y,声明成员函数Point& operator++( );Point operator++( int);以实现对Point类重载“++”(自增)运算符,声明成员函数Point& operator--( );Point operator--( int);以实现对Point类重载“--”(自减)运算符,实现对坐标值的改变。
好的,这里是声明Point类并对其进行运算符重载的代码:
```cpp
class Point {
private:
int x, y;
public:
Point(int x=0, int y=0): x(x), y(y) {}
Point& operator++() {
++x;
++y;
return *this;
}
Point operator++(int) {
Point temp(*this);
++(*this);
return temp;
}
Point& operator--() {
--x;
--y;
return *this;
}
Point operator--(int) {
Point temp(*this);
--(*this);
return temp;
}
};
```
在这个代码中,我们声明了一个Point类,其中包含了两个私有成员变量x和y,它们用于表示点的坐标值。我们还重载了++和--运算符,其中前置++和--运算符会直接对坐标值进行改变,而后置++和--运算符则会返回一个临时变量,并在之后对原对象进行改变。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)