写一个点类Point,每个Point对象有两个私有成员,横坐标和纵坐标。要求如下:

2024年12月05日 00:37
有1个网友回答
网友(1):

//////////////////////////////////////////////////////////////////////////Point.h

#include
#include
using namespace std;

class Point
{
private:
double x,y;
public:
Point();
Point(double x,double y);
Point(Point &p);
bool operator == (Point &p);
bool operator != (Point &p);
Point operator += (Point &p);
Point operator -= (Point &p);
Point operator + (Point &p);
Point operator - (Point &p);
friend inline ostream & operator <<(ostream & os,Point &p)
{
os< os.precision(2); //这两句是控制输出数据小数位数
os<<"此点的x坐标:"< return os;
}
};
////////////////////////////////////////////////////////////////////////////////Point.cpp
#include "Point.h"

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

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

Point::Point(Point &p)
{
this->x = p.x;
this->y = p.y;
}

bool Point::operator == (Point &p)
{
if(this->x == p.x && this->y == p.y)
return true;
else
return false;
}
bool Point::operator != (Point &p)
{
if(this->x != p.x && this->y != p.y)
return true;
else
return false;
}
Point Point::operator += (Point &p)
{
this->x += p.x;
this->y += p.y;
return *this;
}
Point Point::operator -= (Point &p)
{
this->x -= p.x;
this->y -= p.y;
return *this;
}
Point Point::operator + (Point &p)
{
Point t;
t.x = this->x + p.x;
t.y = this->y + p.y;
return t;
}
Point Point::operator - (Point &p)
{
Point t;
t.x = this->x - p.x;
t.y = this->y - p.y;
return t;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////main.cpp
#include "Point.h"

int main()
{
Point p1;
Point p2(2.43,5.33);
Point p3(p2);

cout<
if(p2 == p3)
cout<<"=="< else
cout<<"!="< if(p2 != p1)
cout<<"!="< else
cout<<"=="< p3 += p2;
p1 -= p2;

cout<
Point p4 = p3 + p2;
Point p5 = p2 - p1;

cout<
system("pause");
return 0;
}
这个不给分就太不够意思了。。。。