C++ Creating a function to get distance between two points

12,152

You need to make your Point::getX() and Point::getY() functions const like so:

inline double Point::getX() const
{
    return theX;
}

If they are not const you cannot call them when the parameter is a const reference.

Then the distance is (changed return from void to double):

double distance(const Point & aPoint) const
{
    const double x_diff = getX() - aPoint.getX();
    const double y_diff = getY() - aPoint.getY();
    return std::sqrt(x_diff * x_diff + y_diff * y_diff);
}

I have deliberately not used std::pow since the exponent is 2. You also need to include <cmath> for std::sqrt.

Share:
12,152
Eoin Coogan
Author by

Eoin Coogan

Updated on June 05, 2022

Comments

  • Eoin Coogan
    Eoin Coogan almost 2 years

    In my program I have created a constructor called Point with two values. I also have a set, get, scale and translate function. I'm trying to create a function that allows me to get the distance between the object and another point. I'm have trouble with it though any help would be brilliant.

    #ifndef POINTMODEL
    #define POINTMODEL
    #define POINTDEB UG
    
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    class Point {
    public:
        Point(void);
        Point(double anX, double aY);
        ~Point();
    
        void setPoint(double anX, double aY);
    
        double getX();
        double getY();
    
        double scaleX(double theX);
        double scaleY(double theY);
        void translate(double theX, double theY);
    
        void distance(const Point& aPoint);
    
    protected:
    private:
        double theX;
        double theY;
    };
    
    inline Point::Point(void)
    {
        theX = 1;
        theY = 1;
        cout << "\n The default constructor was called" << endl;
    }
    
    inline Point::Point(double anX, double aY)
    {
        cout << "\n regular constructor called";
    }
    
    inline Point::~Point()
    {
        cout << "\n the destructor was called" << endl;
    }
    
    inline void Point::setPoint(double anX, double aY)
    {
        theX = anX;
        theY = aY;
    }
    
    inline double Point::getX()
    {
        return theX;
    }
    
    inline double Point::getY()
    {
        return theY;
    }
    
    inline double Point::scaleX(double theX)
    {
        return theX;
    }
    
    inline double Point::scaleY(double theY)
    {
        return theY;
    }
    
    inline void Point::translate(double offSetX, double offSetY)
    {
        cout << "X is translated by : " << offSetX << endl;
        cout << "Y is translated by : " << offSetY << endl;
    }
    
    inline void Point::distance(const Point& aPoint)
    {
    }
    
    #endif
    

    Cpp file:

    #include "Point.h"
    
    using namespace std;
    
    int main(void)
    {
        cout << "\n main has started" << endl;
    
        //Point myPoint;
        Point myPoint(1, 1);
    
        myPoint.setPoint(1, 1);
    
        cout << "\n The value for X is : " << myPoint.getX() << endl;
        cout << "\n The value for Y is : " << myPoint.getY() << endl;
    
        cout << "\n X scaled by 2 is : " << myPoint.scaleX(2) << endl;
        cout << "\n Y scaled by 2 is : " << myPoint.scaleY(2) << endl;
    
        myPoint.translate(2, 3);
    
        cout << "\n main has finished" << endl;
    
        return 0;
    }