编写一个点类,需要满足如下要求: 1)数据成员包含x坐标,y坐标 2)定义构造函数和析构函数 3)重载"+"(

2024-12-03 20:41:09
推荐回答(1个)
回答1:

一样的问题啊
就是那几个函数你没有写具体的实现代码
#include
using namespace std;
class Point
{
public:
Point()
{
x=y=0;
}
Point(int,int);
~Point()
{
cout<<"finished"< }
Point(Point &p)
{
x=p.x;
y=p.y;
}
Point operator +(Point &p2);
Point operator -(Point &p2);
void display();

private:
int x;
int y;
};
Point::Point(int a,int b):x(a),y(b)
{}
Point Point::operator +(Point &p2)
{
Point p;
p.x=x+p2.x;
p.y=y+p2.y;
return p;
}
Point Point::operator -(Point &p2)
{
Point p;
p.x=x-p2.x;
p.y=y-p2.y;
return p;
}
ostream&operator<<(ostream&,Point&);
void Point::display()
{
cout<<"("<}
int main()
{
Point p1(3,4),p2(5,-10),p3;
p3=p1+p2;
cout<<"p1=";p1.display();
cout<<"p2=";p2.display();
cout<<"p1+p2=";p3.display();
return 0;
}