结合main函数的代码,编写一个Point类,需要满足如下要求:1)数据成员包含:横坐标,纵坐标2)定义构造函数和析构函数3)重载+(加法)、-(减法)运算符 (提示:相应坐标值的加减)4)重载=(赋值)运算符5)重载> (提取运算符)int main(){ Point p1,p2,p3(3,4); cin>>p2; p1=p2+p3; cout<<p1; p1=p2-p3; cout<<p1; return 0;}
#include <iostream>
using namespace std;
class Point
{
private:
int x,y;
public:
Point(int x,int y);
Point();
~Point();
Point operator+(const Point &a);
Point operator-(const Point &a);
Point& operator=(const Point &a);
friend ostream &operator<<(ostream &os, const Point &a);
friend istream &operator>>(istream &in, Point &a);
};
Point::Point(int x,int y)
{
this->x=x;
this->y=y;
}
Point::Point()
{
Point(0,0);
}
Point::~Point()
{
}
Point Point::operator+(const Point &a)
{
Point tmp;
tmp.x=this->x + a.x ;
tmp.y=this->y + a.y ;
return tmp;
}
Point Point::operator-(const Point &a)
{
Point tmp;
tmp.x=this->x - a.x ;
tmp.y=this->y - a.y ;
return tmp;
}
Point& Point::operator=(const Point &a)
{
this->x=a.x;
this->y=a.y;
return *this;
}
ostream& operator<<(ostream &os, const Point &a)
{
os << "x=" << a.x << " y=" << a.y << endl ;
return os;
}
istream& operator>>(istream &in, Point &a)
{
in >> a.x >> a.y;
return in;
}
int main()
{
Point p1, p2, p3(3, 4);
cin >> p2;
p1 = p2 + p3;
cout << p1;
p1 = p2 - p3;
cout << p1;
return 0;
}
直接写 class Point{private: int x,int y};