Hi so I have a practice lab I am working on where I am asked to fill in the code. The program is supposed to take in two 3D points (x,y,z) and compute the distance between them.

I have almosty all of the program finished im just stuck on how to call the inherited functions in main(). How do I pass my points (a,b,c) and (x,y,z) into the getDistance function? Would something like this work?:

``````cout << Obj3.getDistance(Obj2);
``````

Here is my code:

``````// File name: ~ftp/pub/class/170/ftp/cpp/Inheritance/Exercise.cpp
// Purpose:   Exercise for inheritance lab

#include <iostream>
#include <math.h> // for sqrt()
using namespace std;

class twoD
{
protected:
double x, y; // x and y  coordinates
public:
// inline implementation of constructor
twoD(){x = y = 0.0;} // default constructor
twoD(double i, double j):x(i), y(j){}

// inline implementation of member functions
void setX(double NewX){x = NewX;}
void setY(double NewY){y = NewY;}
double getX() const {return x;}
double getY() const {return y;}
// get distance of 2D points
double getDistance (twoD point) const;
};

// calculate the distance  of two 2D points
double twoD::getDistance(twoD point) const
{
double point1;
double point2;
double dx, dy;
double distance;

point1 = x;
point1 = y;

point2 = point.getX();
point2 = point.getY();

dx = point2 - point1;
dy = point2 - point1;

distance = sqrt(dx * dx + dy * dy);
return distance;
}

class threeD:public twoD
{
private:
double z;
public:

// Create a default inline constructor that reuses the constructor of
// the twoD class.
threeD(double i, double j):twoD(i,j){z = 0.0;}

// Create an inline constructor that initializes the 3D point
// and reuses the twoD class.
threeD(double i, double j, double k):twoD(i,j){z = k;}

void setZ(double NewZ){z = NewZ;}
double getZ() {return z;}

// get distance for two 3D points
double getDistance(threeD point) const;
};

// Overload the definition of getDistance() of twoD class so that it
// can calculate the distance between two 3D points
double threeD::getDistance(threeD point) const
{
double point1;
double point2;
double dx, dy, dz;
double distance;

point1 = x;
point1 = y;
point1 = z;

point2 = point.getX();
point2 = point.getY();
point2 = point.getZ();

dx = point2 - point1;
dy = point2 - point1;
dz = point2 - point1;

distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}

// Implement a main() function.
// You should ask the user for the xyz coordinates of two 3D points,
// and then calculate and print out the distance between these two points.
int main()
{
double a,b,c;
double x,y,z;

cout << "Enter the x,y,z coordinates of point 1: " << endl;
cin >> a >> b >> c;
cout << "Point one is: (" << a << ", " << b << ", " << c << ")" << endl;
cout << endl;

cout << "Enter the x,y,z coordinates of point 2: " << endl;
cin >> x >> y >> z;
cout << "Point two is: (" << x << ", " << y << ", " << z << ")" << endl;
cout << endl;

threeD Obj3(x, y, z);
threeD Obj2(a, b, c);

return 0;
}
``````

## All 2 Replies

Oops! Just realized this line

`cout << Obj3.getDistance(Obj2);`

is correct. Guess I solved my own problem.

Good for you! Another approach would be a function (not class member method) `double distanceBetween(const threeD& ptA, const threeD& ptB)`, which could be used anywhere.It may be more reasonable for general algorithms as well. In any case, this is just a suggestion. Your approach is perfectly reasonable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.