问题及代码:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Point;
class Distance
{
public:
float Dis(Point & p,Point & q);
private:
};
class Point
{
public:
Point (double a=0,double b=0);
double getX();
double getY();
private:
double X;
double Y;
friend Distance;
};
Point ::Point (double a,double b)
{
X=a;
Y=b;
}
double Point ::getX()
{
return X;
}
double Point ::getY()
{
return Y;
}
float Distance::Dis(Point & p,Point & q)
{
return sqrt((p.getX()-q.getX())*(p.getX()-q.getX())+(p.getY()-q.getY())*(p.getY()-q.getY()));
}
int main()
{
float x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
Point p(x1,y1), q(x2,y2);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Distance d;
cout<<d.Dis(p,q)<<endl;
return 0;
}
运行结果: