Home > Software design >  Program to calculate distance between two points in 3D with class and operator overload [duplicate]
Program to calculate distance between two points in 3D with class and operator overload [duplicate]

Time:09-22

I can't speak english very well, sorry. I am having a problem with a practice question given to me. I am asked to write a code that finds the distance between 2 points in 3D space according to their x, y and z coordinates. But while doing this, it wants me to use the class structure, use the get-set functions, use the concepts of public and private, and overload the "*" operator to find the distance. I wrote a code and it gives correct result but it does not meet the desired conditions. I would really appreciate if you could help. thank you so much.

    #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;
    }

CodePudding user response:

Below is the complete working example. Note the use of double instead of 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;
    }


CodePudding user response:

  • #include <cmath> not math.h
  • Make coordinat store only one point (x, y, z).
  • Add a member function for subtracting one coordinat from an other:
    coordinat& operator-=(const coordinat& rhs) {
         // add code to subtract the values in rhs from the values stored in *this 
         return *this; 
    }
    
  • Add a member function to return a coordinat's distance from origo
    double length() const { return std::sqrt(x * x   y * y   z * z); }
    
  • Add a free function to subtract two coordinat's, returning a new coordinat:
    coordinat operator-(const coordinat& lhs, const coordinat& rhs) {
         coordinatrv(lhs); // copy
         rv -= rhs;     // use the member function "operator-="
         return rv;
    }
    

With these additions, you can ask the user for input:

coordinat a, b;

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

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

and calculate the distance:

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