Point类改变坐标值

定义一个Point类,有坐标 x, y 两个成员变量;对Point类重载“++”、“- -”运算符,实现坐标值的改变。
下面我简单写了一下,希望有不当的地方呢能够指出,谢谢。
【完整代码】

#include<iostream>
using namespace std;
 
class Point 
{
private:
	int x,y;
public:
	//在此改变要输入 x,y的值 
	Point()
	{
		x=1;
		y=1;
	}
	//前置 
	Point& operator ++();
	Point& operator --();
	//后置 
	Point operator ++(int);
	Point operator --(int);
	void print()
	{
		cout<<"point:"<<x<<","<<y<<endl;
	}
};
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()
{
	Point xy1;
	cout << "两个点初始坐标:" << endl;
	xy1.print();
	cout << "前置运算后的坐标:" << endl;
	++xy1;
	xy1.print();
	--xy1;
	xy1.print();
	cout << "后置运算后的坐标:" << endl;
	xy1++;
	xy1.print();
	xy1--;
	xy1.print();
}
发布了35 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43873385/article/details/103975660
今日推荐