You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于类与运算符重载的3D空间两点距离计算程序开发问题

Fixing Your 3D Point Distance Code to Meet Requirements

Hey there! No worries about your English at all—let's get your code aligned with the problem's requirements. Your current code works, but it doesn't follow the OOP design rules the problem asks for (separate point objects, get/set methods, operator overloading). Let's fix this step by step.

First, Let's Break Down What Your Original Code Is Missing

  • Your coordinat class stores two points in one class, which doesn't follow good object-oriented practice (each point should be its own object)
  • You don't have proper getter/setter methods for private members—instead, you're reading all values in one get() function
  • You're using a calculate() method instead of overloading the * operator to compute distance

Here's the Revised Code That Meets All Requirements

#include <iostream>
#include <cmath> // Prefer cmath over math.h for standard C++
using namespace std;

class Point3D {
private:
    // Private coordinates: only accessible via class methods
    double x;
    double y;
    double z;

public:
    // Optional constructor to initialize a point with values (or defaults to 0)
    Point3D(double xVal = 0, double yVal = 0, double zVal = 0) 
        : x(xVal), y(yVal), z(zVal) {}

    // Getter methods: safely retrieve private coordinate values
    double getX() const { return x; }
    double getY() const { return y; }
    double getZ() const { return z; }

    // Setter methods: safely modify private coordinate values
    void setX(double xVal) { x = xVal; }
    void setY(double yVal) { y = yVal; }
    void setZ(double zVal) { z = zVal; }

    // Overload the * operator to calculate distance between two points
    double operator*(const Point3D& other) const {
        double dx = other.getX() - this->getX();
        double dy = other.getY() - this->getY();
        double dz = other.getZ() - this->getZ();
        // Using direct squaring instead of pow() for better efficiency
        return sqrt(dx*dx + dy*dy + dz*dz);
    }
};

// Helper function to get user input for a point (keeps main clean)
void inputPoint(Point3D& point, const string& pointName) {
    double x, y, z;
    cout << pointName << " point X coordinate: ";
    cin >> x;
    cout << pointName << " point Y coordinate: ";
    cin >> y;
    cout << pointName << " point Z coordinate: ";
    cin >> z;
    // Use setters to update the point's private values
    point.setX(x);
    point.setY(y);
    point.setZ(z);
}

int main() {
    Point3D point1, point2;

    // Get input for both points
    inputPoint(point1, "First");
    inputPoint(point2, "Second");

    // Use the overloaded * operator to compute distance
    double distance = point1 * point2;
    cout << "\nDistance between the two points: " << distance << endl;

    return 0;
}

Let's Verify It Meets All Requirements

  • Class Structure: We use a Point3D class to represent a single 3D point—this is the correct OOP approach, as each point is a distinct object.
  • Get-Set Functions: The getX()/getY()/getZ() methods retrieve private coordinates, and setX()/setY()/setZ() modify them, enforcing controlled access to private data.
  • Public/Private Access Control: The x, y, z variables are private, so they can't be modified directly from outside the class—only through the public setter methods.
  • Overloaded * Operator: The operator* method lets us write point1 * point2 to calculate the distance between the two points, exactly as the problem requires.

Small Improvements I Added

  • Used double instead of int for coordinates—this allows for non-integer points (like (1.5, 2.3, 4.7)) which is more realistic for 3D space.
  • Replaced pow() with direct squaring (dx*dx)—it's faster and avoids any floating-point precision issues from the pow function.
  • Used cmath instead of math.h—this is the standard header for C++ mathematical functions.

内容的提问来源于stack exchange,提问作者Cheddar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:12:39