作业帮 > 综合 > 作业

定义一个点类,输入坐标上的两个点,计算出两点之间的距离,并输出

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/05/12 13:48:03
定义一个点类,输入坐标上的两个点,计算出两点之间的距离,并输出
我是c++初学者,完全不知道类是个什么东西
定义一个点类,输入坐标上的两个点,计算出两点之间的距离,并输出

#include <iostream>#include <cmath>//因为要使用sqrt函数using namespace std;class Point{public:    Point(float x_, float y_):x(x_),y(y_){}    friend float distance_(Point &, Point &);private:    float x;    float y;};
float distance_(Point & A, Point & B){    return sqrt((A.y-B.y)*(A.y-B.y)+(A.x-B.x)*(A.x-B.x));}
int main(){    Point a(3.0, 4);    Point b(0, 0);    cout << "a点与b点间的距离是:" << distance_(a, b) <<endl;    return 0;}