使用 class 和运算符重载计算 3D 中两点之间距离的程序

Program to calculate distance between two points in 3D with class and operator overload

我英语说得不太好,抱歉。我对给我的练习题有疑问。我被要求编写一个代码,根据它们的 x、y 和 z 坐标找到 3D space 中 2 个点之间的距离。但是在这样做的同时,它要我使用 class 结构,使用 get-set 函数,使用 public 和 private 的概念,并重载“*”运算符来查找距离。我写了一个代码,它给出了正确的结果,但它不符合所需的条件。如果您能提供帮助,我将不胜感激。非常感谢。

    #include<iostream>
    #include<math.h>
    using namespace std;

    class coordinat
    {
    private:
        int x1, y1, z1, x2, y2, z2;
    public:
        void get()
        {
            cout << "1. point X value: ";
            cin >> x1;

            cout << "1. point Y value: ";
            cin >> y1;

            cout << "1. point Z value: ";
            cin >> z1;

            cout << "2. point X value: ";
            cin >> x2;

            cout << "2. point Y value: ";
            cin >> y2;

            cout << "2. point Y value: ";
            cin >> z2;
        }

        void calculate()
        {
            float distance;
            distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2));
            cout << "Distance between 2 points: " << distance << endl;
        }
    };

    int main()
    {
        coordinat c;
        c.get();
        c.calculate();

        return 0;
    }
  • #include <cmath> 不是 math.h
  • 使coordinat只存储一个点(xyz)。
  • 添加一个成员函数,用于从另一个中减去一个 coordinat
    coordinat& operator-=(const coordinat& rhs) {
         // add code to subtract the values in rhs from the values stored in *this 
         return *this; 
    }
    
  • 添加一个成员函数到return一个coordinat距离origo的距离
    double length() const { return std::sqrt(x * x + y * y + z * z); }
    
  • 添加一个免费函数来减去两个 coordinat,return创建一个新的 coordinat
    coordinat operator-(const coordinat& lhs, const coordinat& rhs) {
         coordinatrv(lhs); // copy
         rv -= rhs;     // use the member function "operator-="
         return rv;
    }
    

通过这些添加,您可以要求用户输入:

coordinat a, b;

std::cout << "Enter data for point 1:\n";
a.get();

std::cout << "Enter data for point 2:\n";
b.get();

并计算距离:

coordinat distance = a - b;
std::cout << distance.length();

下面是完整的工作示例。注意使用 double 而不是 int.

#include<iostream>
#include <tuple>
#include<math.h>
using namespace std;

    class coordinate
    {
    private:
        
        double x1, y1, z1;
    public:
        //this is a setter
        void set(double _x1, double _y1, double _z1)
        {
            x1 = _x1;
            y1 = _y1;
            z1 = _z1;
            
            
        }
        //this is a getter
        double get_x()
        {
            return x1;
        }
        double get_y()
        {
            return y1;
        }
        double get_z()
        {
            return z1;
        }
        //overload operator*
        double operator*(coordinate const &rhs) 
        {
            
            return sqrt(pow(rhs.x1 - x1, 2) + pow(rhs.y1 - y1, 2) + pow(rhs.z1 - z1, 2));
        }
       
    };

    int main()
    {
       coordinate c1, c2;
        //use the settter
        c1.set(7,0,2);
        //use the getter
        std::cout<<"x position of c1: "<<c1.get_x()<<std::endl;
        std::cout<<"y position of c1: "<<c1.get_y()<<std::endl;
        std::cout<<"z position of c1: "<<c1.get_z()<<std::endl;
       
        //use the setter
        c2.set(2,6,0);
        //used the getter
        std::cout<<"x position of c2: "<<c2.get_x()<<std::endl;
        std::cout<<"y position of c2: "<<c2.get_y()<<std::endl;
        std::cout<<"z position of c2: "<<c2.get_z()<<std::endl;
        
        //use the overloaded operator* to calculate distance
        double distance = c1*c2;
        std::cout<<"The distance between the two points is: "<<distance<<std::endl;

        return 0;
    }