定义一个点类,计算点(-3,5)和(2,-2)间的距离

来源:百度知道 编辑:UC知道 时间:2024/09/27 19:23:19
用C++语言实现,望各位高手指点!

#include<iostream>
using namespace std;
#include<math.h>

class Point
{
double x;
double y;
public:
Point(long xx, long yy) : x(xx),y(yy){}
double dis(const Point & p) const;
};

double Point::dis(const Point & p) const
{
return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}

int main()
{
Point a(-3,5),b(2,-2);
cout<<"Dis of a(-3,5) and b(2,-2) is:"<<a.dis(b)<<endl;
return 0;
}

上面这种方法可以,,你也可以用用类组合啊
都差不多的#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
Point(int xx=0,int yy=0){X=xx;Y=yy;}
friend class Line;
private:
int X,Y;
};
class Line
{
public:
Line (Point xp1,Point xp2);
double GetLen(){return len;}
private:
Point p1,p2;
double len;
};
Line::Line(Point xp1,Point xp2):p