求两点间的距离,帮忙补全程序。。。

来源:百度知道 编辑:UC知道 时间:2024/06/10 00:41:40
现行程序如下:
// Point.h

class Point {
private:
int X;
int Y;
public:
Point(int x,int y) {X=x,Y=y;}
void distance(const Point &a,const Point &b);
};

// Point.cpp

#include <iostream>
#include <cmath>
#include "Point.h"
using namespace std;

int main()
{
Point A(1,2);
Point B(3,4);

return 0;
}

void Point::distance(const Point &a,const Point &b)
{
double len;
len=sqrt((a.X-b.X)*(a.X-b.X) +(a.Y-b.Y)*(a.Y-b.Y));
cout<<"The distance is "<<len<<endl;
}

要求:计算A(1,2),B(3,4)两点间的距离,在屏幕上输出。

// Point.h

class Point {
private:
int X;
int Y;
public:
Point(int x,int y) {X=x,Y=y;}
void distance(const Point &a,const Point &b);
};

// Point.cpp

#include <iostream>
#include <cmath>
#include "Point.h"
using namespace std;

int main()
{
Point A(1,2);
Point B(3,4);
A.distance(A,B);//加这一行就行
return 0;
}

void Point::distance(const Point &a,const Point &b)
{
double len;
len=sqrt((a.X-b.X)*(a.X-b.X) +(a.Y-b.Y)*(a.Y-b.Y));
cout<<"The distance is "<<len<<endl;
}