#include <iostream>
#include <cmath>

using namespace std;

class Point {
private:
    double x, y;
public:
    Point(double x = 0, double y = 0) {
        this->x = x;
        this->y = y;
    }
    void set(double x, double y) {
        this->x = x;
        this->y = y;
    }
    void show() {
        cout << '(' << x << ", " << y << ')' << endl;
    }
    friend double distance(Point p1, Point p2) {
        double dx = p1.x - p2.x;
        double dy = p1.y - p2.y;
        return sqrt(dx * dx + dy * dy);
    }
};

int main() {
    Point p1(1, 2), p2(3, 4);
    cout << "p1 = ";
    p1.show();
    cout << "p2 = ";
    p2.show();
    cout << "distance(p1, p2) = " << distance(p1, p2) << endl;
    return 0;
}
C++ 点类 Point 实现及距离计算 -  友元函数应用

原文地址: https://www.cveoy.top/t/topic/nXJo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录