#include <cmath>

class Point {
public:
    Point(double x, double y) : x(x), y(y) {}

    Point operator+(const Point& other) const {
        double newX = x + other.x;
        double newY = y + other.y;
        return Point(newX, newY);
    }

    double operator-(const Point& other) const {
        double deltaX = x - other.x;
        double deltaY = y - other.y;
        return std::sqrt(deltaX * deltaX + deltaY * deltaY);
    }

private:
    double x;
    double y;
};
C++ 点类实现:重载运算符 '+' 和 '-' 进行加法和距离计算

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

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