2D/3D point Distance to origin Problem

Hello, I am trying to write a program that calls methods from a base class 2D point and uses these methods in a derived 3D point class. I believe I have all of the code correct except the distanceToOrigin() in my 3D class. I do not know how to incorporate the function from the base class and add the point z.


Problem @ line 53 of Point3D.cpp

Point2D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * Point2D.h
 *
 *  Created on: Jan 17, 2018
 *      Author: kamilla
 */

#ifndef POINT2D_H_
#define POINT2D_H_

class Point2D {
private:
	double x;
	double y;
	double distance;
	double xcord;
	double ycord;
public:
	Point2D();
	Point2D(int x, int y);
	Point2D(Point2D& point);

	double  getX();
	double  getY();
	void    getPoint(double& x1, double& y1);
	void    setX(double x);
	void    setY(double y);
	void    setPoint(double x1, double y1);
	double  distanceToOrigin();
	void    print();

	virtual ~Point2D();
};

#endif /* POINT2D_H_ */ 


Point2D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

ifstream fin;

Point2D::Point2D()
{
	x = 0;
	y = 0;
	distance = 0;
}

Point2D::Point2D(int x, int y)
{
    xcord = x;
    ycord = y;
}


Point2D::Point2D(Point2D& other)
{
	x = other.x;
	y = other.y;
	distance = 0;
	x = 0;
	y = 0;
}

double  Point2D::getX()
{
	fin>> x;
}

double  Point2D::getY()
{
	fin>> y;
}

void Point2D::getPoint(double& x1,double& y1)
{
    fin>>x1>>y1;
}

void    Point2D::setX(double x)
{
	this->x = x;
}

void    Point2D::setY(double y)
{
	this->y = y;
}

void    Point2D::setPoint(double x1, double y1)
{
	this->x = x1;
	this->y = y1;
}

double  Point2D::distanceToOrigin()
{
	 distance = sqrt(((x * x) + (y * y)));
}

void    Point2D::print()
{
	std::cout << "Distance to origin =" << distance;
}

Point2D::~Point2D()
{}


Point3D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef POINT3D_H_
#define POINT3D_H_

#include "Point2D.h"
#include <fstream>
using namespace std;

class Point3D : public Point2D
{
private:
	double z;
	double zcord;
	double z1;
	double distance1;
public:
	Point3D();
	Point3D(double x, double y, double z);
	Point3D(Point3D& point);

	double  getZ();
	void    getPoint(double& x1, double& y1, double& z1);
	void    setZ(double z);
	void    setPoint(double x1, double y1, double z1);
	double  distanceToOrigin();
	void    print();

	virtual ~Point3D();
};

#endif /* POINT3D_H_ */ 


Point3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Point2D.h"
#include "Point3D.h"

#include <cmath>
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin;

Point3D::Point3D() : Point2D()
{
	z = 0;
}

Point3D::Point3D(double x1, double y1, double z1) :
         Point2D(x1, y1)
{
	zcord = z;
}

Point3D::Point3D(Point3D& other) :
         Point2D(other)
{
	z = other.z;
}

double  Point3D::getZ()
{
	fin >> z;
}

void Point3D::getPoint(double& x1,double& y1, double& z1 )
{
	Point2D::getPoint( x1, y1);
	fin >> z1;
}

void    Point3D::setZ(double z1)
{
	this->z = z1;
}
void    Point3D::setPoint(double x1, double y1, double z1)
{
	Point2D::setPoint( x1, y1);
	this->z = z1;

}

double  Point3D::distanceToOrigin()
{
	Point2D::distanceToOrigin();
	distance1 = sqrt( + z*z);
}

void    Point3D::print()
{
	std::cout << "Distance to origin =" << distance1;
}

Point3D::~Point3D()
{}



Last edited on
Square the 2D distance, add the squared third coordinate, then get the square root.

It helps to have a distanceSq() member, which returns the sum of the squared coordinates without calling sqrt(), which is an expensive call and not needed in many situations (such as this one).
Yeah I understand how to solve the problem and tried entering it as:

distance1 = sqrt ( (Point2D.distanceToOrigin() * Point2D.distanceToOrigin()) + z * z)

but I know that code is incorrect because distance, x, and y are all private variables in class Point2D, and am just wondering what format I would write that as?
Thank you!
Last edited on
I don't understand what the problem is. What does it matter that those members are private if you don't need them to call Point2D::distanceToOrigin()? Why do you think that code is incorrect (technically it's syntactically invalid, but it's almost valid)?
It keeps giving me an error message saying I can't use it but I just made distance a public member and it worked. Thanks for helping!
It keeps giving me an error message saying ...

Hearsay. Please show the error messages verbatim. Exact. The details are important.


The compiler can warn about some things that are suspicious, even though not completely illegal. For example:
1
2
3
4
double  Point2D::distanceToOrigin()
{
	 distance = sqrt(((x * x) + (y * y)));
}

This function declares that is does return a double value. A good compiler (options) would warn you that the body of the function does not have a value returning statement.

If I would use that class, I should not have to look at the implementation. I should be able to trust on the interface. What I see on the interface makes me think that this is how points work:
1
2
3
4
5
6
7
#include <iostream>
#include <Point2D.h>

int main() {
  Point2D fubar( 7, 42 );
  std::cout << "Distance: " << fubar.distanceToOrigin() <<'\n';
}

Well, is it?
Why not just virtual in the base and override in the derived?
Also why are your x and y private instead of protected?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cmath>

class Point2D
{
public:
    Point2D(double x, double y) : x_(x), y_(y)
    {        
    }
    virtual double DistanceToOrigin()
    {
        return std::sqrt(x_*x_ + y_*y_);
    }

protected:
    double x_;
    double y_;
};

class Point3D : public Point2D
{
public:
    Point3D(double x, double y, double z) :
        Point2D(x,y),
        z_(z)
    {        
    }
    double DistanceToOrigin() override
    {
        return std::sqrt(x_*x_ + y_*y_ + z_*z_);
    }

protected:
    double z_;
};

int main() 
{
    using std::cout;
    using std::endl;

    Point2D p2(9, 12);
    Point3D p3(9, 12, 20);

    cout << p2.DistanceToOrigin() << endl;  //15
    cout << p3.DistanceToOrigin() << endl;  //25
}


ez game
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
using namespace std;

class Point
{
   double x = 0, y = 0, z = 0;

public:
   Point( double X = 0, double Y = 0, double Z = 0 ) : x( X ), y( Y ), z( Z ) {}
   double distanceToOrigin() { return sqrt( x * x + y * y + z * z ); }
};


int main() 
{
   Point p0;
   Point p1( 9 );
   Point p2( 9, 12 );
   Point p3( 9, 12, 20);

   cout << p0.distanceToOrigin() << '\n';
   cout << p1.distanceToOrigin() << '\n';
   cout << p2.distanceToOrigin() << '\n';
   cout << p3.distanceToOrigin() << '\n';
}
Or something like:
1
2
3
4
5
6
7
8
9
template <size_t N>
class Point {
  double data[N] {};
public:
  // interface
};

using Point2D = Point<2>;
using Point3D = Point<3>;
Topic archived. No new replies allowed.