C++ 点类实现:重载运算符 '+' 和 '-' 进行加法和距离计算
#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;
};
原文地址: https://www.cveoy.top/t/topic/kWeo 著作权归作者所有。请勿转载和采集!