Question

For a homework assignment we are asked to create two classes: a MyPoint class and a ThreeDPoint class.

Here is my MyPoint class:

#pragma once 
#include <math.h>

class MyPoint
{
private:
    double x, y;

public:
    MyPoint()
    {
        x = y = 0;
    }

    MyPoint(double x, double y)
    {
        this->x = x;
        this->y = y;
    }

    double getX() 
    {
        return x;
    }

    double getY() 
    {
        return y;
    }

    double distance(MyPoint pointB)
    {
        return sqrt((x - pointB.x) * (x - pointB.x) 
            + (y - pointB.y) * (y - pointB.y));
    }
};

Here is my ThreeDPoint class:

#pragma once
#include "MyPoint.h"

class ThreeDPoint : public MyPoint
{
private:
    double z;

public:
    ThreeDPoint() // : MyPoint()
    {
        z = 0;
    }

    ThreeDPoint(double x, double y, double z) : MyPoint(x, y)
    {
        this->z = z;
    }

    double getZ()
    {
        return z;
    }

    double distance(ThreeDPoint pointB) // function overloading
    {
        // x and y are private not protected
        return sqrt((getX() - pointB.getX()) * (getX() - pointB.getX()) 
            + (getY() - pointB.getY()) * (getY() - pointB.getY()) 
            + (getZ() - pointB.getZ()) * (getZ() - pointB.getZ()));
    }
};

And here is main:

#include <iostream>
#include "SignatureBlock.h"
#include "MyPoint.h"
#include "ThreeDPoint.h"

int main()
{
    SignatureBlock myBlock;
    std::cout << myBlock.toString();

    MyPoint pointA(1, 2);
    MyPoint pointB(4, 2.5);

    std::cout << pointA.distance(pointB) << '\n';

    ThreeDPoint point_a(0, 0, 0);
    ThreeDPoint point_b(10, 30, 25.5);

    std::cout << point_a.distance(point_b) << '\n';

    return 0;
}

This code works fine. It is able to calculate the distance between two points in both 2-dimensional and 3-dimensional space. However, in the homework instructions for the ThreeDPoint class, the book says to create

"A constant get function that returns the z value"

and

"A constant distance(const MyPoint&) function to return the distance between this point and the other point in three-dimensional space."

My question is what do they mean by "A constant get function..." and "A constant distance(const MyPoint&) function..."? I understand that you can put const after a function declaration to prevent the function from changing any class members, but why is this necessary here?

Also, why would I pass const MyPoint& to the distance function rather than a ThreeDPoint object? After all we're trying to find the distance between two ThreeDPoints. I read that if you have a parent object parameter in the member function declaration any child of that parent can also be passed to the function, so maybe that has something to do with it.

Lastly, isn't the distance(const MyPoint&) in the instructions missing a name for the MyPoint& object? I did try to type this in and the compiler doesn't mind, but how would I access the members of the object being passed in?

Edit: Also, what purpose does the & serve?

Most importantly, how would I re write my code to follow exactly what the instructions state?

Thanks for reading and thank you for any suggestions anyone might have.

Was it helpful?

Solution

If you live in a world where there are no const objects, it's never necessary to mark methods const. However, it is a good idea. Using const pointers and references is a very good practice when you don't need to change members of an object, and it can help you find design flaws when your program gets bigger.

It's also a good practice to pass references to objects, because passing a reference is usually faster than passing a copy of the object. (It's also in this context that you'll use const references: it tells the caller that you want the object by reference because it's faster, not because you want to change it.)

It's legal to omit argument names, but it has the obvious consequence that you won't be able to use them in your function. It's also a common notation because compilers get rid of argument names when they create function signatures, so you'll often see error messages that look like distance(const MyPoint&) instead of distance(const MyPoint& point).

I can't help you about why they want a const MyPoint& for the ThreeDPoint class. My best guess is that they want you to assume the z component of that point is 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top