0

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;
    }
Cheddar
  • 11
  • 1
  • 2
  • FYI: [SO: What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/q/4421706/7478597), [C++ Overloading (Operator and Function)](https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm) – Scheff's Cat Sep 19 '21 at 05:24
  • Are you sure that your `get()` function is supposed to read from `std::cin`? Usually, I would expect that a `get()` member function returns a member, and a `set()` member function modifies a member (in a permitted way). Furthermore, I would expect that you have to write a `class Point` which stores _one_ triple of coordinates, while your `operator*()` has to compute the distance for two instances of `class Point` (using the `getX()`/`getY()`/`getZ()` member functions to retrieve the coordinate values from each instance). – Scheff's Cat Sep 19 '21 at 05:29
  • @Scheff'sCat If you have time, can you write a sample code for better understanding? By examining the code, I can get a clear idea of how I should do it. – Cheddar Sep 19 '21 at 05:40
  • I suspect if I write an answer you will copy it and pass it to your teacher. Sorry, this is no Do-my-homework-site. - The sample on the Tutorial Points site (that with these boxes) is quite close to what you intend to do, and it's different enough that you cannot do copy-paste programming. Actually not the worst case to learn that stuff... – Scheff's Cat Sep 19 '21 at 05:41
  • And, btw., please, keep in mind that your teacher has Internet too and probably knows how to google as well... ;-) – Scheff's Cat Sep 19 '21 at 05:45
  • @Scheff'sCat I don't want you to answer my question. I'm talking about an example code in the structure you mentioned. A code that calculates distance is not required, I'm talking about any code just to understand it more clearly. The first question won't get me a grade, my goal is to better understand the structures. As I said in the question, I don't know English very well. For this reason, I have some difficulty in understanding the theoretical explanations. – Cheddar Sep 19 '21 at 05:51
  • I try to understand more by examining examples and practicing. It's okay for my teacher to see this, because it's not an assignment, and answering this question won't earn me a grade. – Cheddar Sep 19 '21 at 05:52

2 Answers2

1
  • #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();
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
-1

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


Jason
  • 36,170
  • 5
  • 26
  • 60