编程:Point类有成员变量x,y,运算符重载++、--实现对坐标改变

来源:百度知道 编辑:UC知道 时间:2024/05/31 00:22:11
具体要求如下:定义Point类,有坐标x,y两个整形的私有成员变量;定义成员函数Point& operator++();Point operator++(int);以实现对Point类重载“++”运算符,定义成函数Point& operator –-();Point operator - -(int);以实现对Point类重载“- -”运算符,实现对坐标值的改变。

/*
定义Point类
有坐标x,y两个成员变量,
对Point类重载 “++” (自增),”--”(自减)运算符,实现对坐标值的改变
包含前置与后置
*/

#include <iostream>
using namespace std;

class Point{
public:
Point(){ }
Point(int x,int y);
~Point(){ }
Point operator++(int);//对应于a++
Point operator--(int);//对应于a--
friend ostream& operator<<(ostream& out, const Point& a);//友元函数cout<<a
friend void operator>>(istream&in, Point& a);//友元函数cin>>a
public:
int x;
int y;
};

Point::Point(int x,int y){
this->x=x;
this->y=y;
}

Point& Point::operator++(){//++a
this->x++;
this->y++;
return *this;
}

Point Point::operator++(int){//a++
Point tmp(this->x,this->y);
this->x++;
this->y++;
return tmp;
}

Point& Point::operator--(){//--a
this->x--;
this->y--;
return *