求两点之间距离 C++

 求两点之间距离(20 分)

定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数。 定义一个函数Distance(), 用于求两点之间的距离。
输入格式:

输入有两行: 第一行是第一个点的x坐标和y坐标; 第二行是第二个点的x坐标和y坐标。
输出格式:

输出两个点之间的距离,保留两位小数。
输入样例:

0 9 3 -4
输出样例:

13.34

#include<iostream>  
#include<cmath>  
#include<stdio.h>
using namespace std;  
  
class Point{  
private:  
    double x,y;  
public:  
    Point(double x,double y)  
    {  
        this->x = x;  
        this->y = y;  
    }  
    double Getx()  
    {  
        return x;  
    }  
    double Gety()  
    {  
        return y;  
    }  
      
    double Distance(const Point &p)  //定义拷贝构造函数  
    {  
        x -= p.x;  
        y -= p.y;  
        return sqrt(x*x+y*y);  
    }  
    void ShowPoint()  
    {  
        cout << "<" << Getx() << "," << Gety() << ">" << endl;  
    }  
};  
  
int main()  
{  
    double x1,y1,x2,y2;  
    double x;

    cin >> x1 >> y1 >> x2 >> y2;  
    Point P1(x1,y1);  
    Point P2(x2,y2);  

      x=P1.Distance(P2);
      cout.precision(2);
      cout.setf(ios::fixed);
    cout << x << endl;  
    
    return 0;  
}  
原文地址:https://www.cnblogs.com/chuijingjing/p/9032984.html