编写程序声明Point类,在类中声明整型的私有成员变量x、y,声明成员函数Point& operator++( );Point operator++( int);以实现对Point类重载“++”(自增)运算符,声明成员函数Point& operator--( );Point operator--( int);以实现对Point类重载“--”(自减)运算符,实现对坐标值的改变。#include<iostream> using namespace std; class Point { private: int x,y; public: Point(int nx=0,int ny=0):x(nx),y(ny){ } Point& operator++( ); Point operator++(int); Point& operator--( ); Point operator--(int); int getx(){return x;} int gety(){return y;} }; ........ int main() { int m,n; cin>>m>>n; Point a(m,n); cout<<"a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; a++; cout<<"after a++,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; ++a; cout<<"after ++a,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; a--; cout<<"after a--,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; --a; cout<<"after --a,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; return 0; }补齐代码
时间: 2024-01-19 18:04:55 浏览: 86
下面是完整的代码:
```c++
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int nx = 0, int ny = 0) : x(nx), y(ny) { }
Point& operator++();
Point operator++(int);
Point& operator--();
Point operator--(int);
int getx() { return x; }
int gety() { return y; }
};
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;
}
int main()
{
int m, n;
cin >> m >> n;
Point a(m, n);
cout << "a的值为:" << a.getx() << " , " << a.gety() << endl;
a++;
cout << "after a++,a的值为:" << a.getx() << " , " << a.gety() << endl;
++a;
cout << "after ++a,a的值为:" << a.getx() << " , " << a.gety() << endl;
a--;
cout << "after a--,a的值为:" << a.getx() << " , " << a.gety() << endl;
--a;
cout << "after --a,a的值为:" << a.getx() << " , " << a.gety() << endl;
return 0;
}
```
运行结果:
```
3 5
a的值为:3 , 5
after a++,a的值为:4 , 6
after ++a,a的值为:5 , 7
after a--,a的值为:4 , 6
after --a,a的值为:3 , 5
```
阅读全文