In: Computer Science
(C++) How do I go about obtaining the origin of a point via an instance variable?
I'm fairly new to C++ and one of my first assignments in my current class has a focus on us trying to understand headers, implementations, pointers, constructors, and the like. This particular assignment has us working with x and y coordinates. I've gotten most of this completed, but I'm not quite sure what I've done so far is going to work and there are a few parts I've completely stuck on; I haven't even been able to test this yet. Also, I apologize in advance if I'm not using the proper terminology for some of this.
There are two headers, and here is an abstract one (Point.h) that doesn't require me to edit it, but is important as a reference to the entire assignment:
using length_unit = double;
using coordinate_unit = double;
/**
* A Point Interface.
*/
class Point {
public:
/**
* Calculate the
distance this Point is from the origin.
* @return The
distance this Point is from the origin is returned.
*/
virtual length_unit
DistanceFromOrigin() const = 0;
/**
* Calculate the
distance this Point is from the given Point.
* @param other
another Point in the plane
* @return The
distance this Point is from the given Point is returned.
*/
virtual length_unit
DistanceFromPoint(const Point &other) const = 0;
virtual ~Point() =
default;
};
Here's another header (TwoDimensionalPoint.h) that I was only required to add private data members with the stipulation that they have to pointers:
#include "Point.h"
/**
* An implementation of the Point interface
in a two-dimensional plane.
*/
class TwoDimensionalPoint : public Point {
private:
// TODO: Add appropriate
data members
coordinate_unit
*x_coordinate;
coordinate_unit
*y_coordinate;
public:
/**
*
Default/initializing constructor.
* @param
x_coordinate the x-coordinate of this Point
* @param
y_coordinate the y-coordinate of this Point
*/
explicit
TwoDimensionalPoint(coordinate_unit x_coordinate = 0,
coordinate_unit y_coordinate = 0);
/* The Big Five */
/**
* Destructor
responsible for deleting memory occupied by the coordinates of this
Point.
*/
~TwoDimensionalPoint()
override;
/**
* Copy constructor
used to create a new Point with the same coordinates of the other
Point.
* @param other a
Point used as a template for creating this point using copy
semantics
*/
TwoDimensionalPoint(const TwoDimensionalPoint &other);
/**
* Move constructor
used to create a new Point with the same coordinates of the other
Point.
* @param other a
Point used as a template for creating this point using move
semantics
*/
TwoDimensionalPoint(TwoDimensionalPoint &&other)
noexcept;
/**
* Copy assignment
operator used to create a new Point with the same coordinates as
the other Point.
* @param rhs an
l-value; the right-hand side of the assignment statement lhs =
rhs
* @return A new
Point with the same coordinates as the given (l-value)
TwoDimensionalPoint is returned.
*/
TwoDimensionalPoint
&operator=(const TwoDimensionalPoint &rhs);
/**
* Move assignment
operator used to create a new Point with the same coordinates as
the other Point.
* @param rhs an
r-value; the right-hand side of the assignment statement
TwoDimensionalPoint lhs = TwoDimensionalPoint{x, y}
* @return A new
Point with the same coordinates as the given (r-value)
TwoDimensional Point is returned.
*/
TwoDimensionalPoint
&operator=(TwoDimensionalPoint &&rhs);
/* Inherited Point
behavior */
/**
* @copydoc
Point::DistanceFromOrigin() const
*/
length_unit
DistanceFromOrigin() const override;
/**
* @copydoc
Point::DistanceFromPoint(const Point &other) const
*/
length_unit
DistanceFromPoint(const Point &other) const override;
/* Specific
TwoDimensionalPoint behavior */
/**
* X-coordinate
accessor method.
* @return The
x-coordinate of this Point is returned.
*/
virtual length_unit
GetX() const;
/**
* Y-coordinate
accessor method.
* @return The
y-coordinate of this Point is returned.
*/
virtual length_unit
GetY() const;
/**
* Assess whether
this Point is the origin.
* @return True if
this Point is the origin, otherwise false.
*/
virtual bool IsOrigin()
const;
/**
* Translates this
Point by the given dimensions.
* @param x the
amount to translate in the x-direction
* @param y the
amount to translate in the y-direction
*/
void
Translate(coordinate_unit x = 0, coordinate_unit y = 0);
};
And then finally the implementation (TwoDimensionalPoint.cpp) is the meat and potatoes of what I need to do; I have to complete the areas with commented with "TODO":
#include <algorithm>
#include <cmath>
#include "TwoDimensionalPoint.h"
TwoDimensionalPoint::TwoDimensionalPoint(coordinate_unit
x_coordinate, coordinate_unit y_coordinate) {
// TODO: Add an
appropriate initializer list
x_coordinate = new
coordinate_unit{ x_coordinate };
y_coordinate = new
coordinate_unit{ y_coordinate };
}
TwoDimensionalPoint::~TwoDimensionalPoint()
{
// TODO: Delete
appropriate data members
delete x_coordinate,
y_coordinate;
}
TwoDimensionalPoint::TwoDimensionalPoint(const
TwoDimensionalPoint &other) {
// TODO: Add an
appropriate initializer list
x_coordinate = new
coordinate_unit{ *other.x_coordinate };
y_coordinate = new
coordinate_unit{ *other.y_coordinate };
}
TwoDimensionalPoint::TwoDimensionalPoint(TwoDimensionalPoint
&&other) noexcept {
// TODO: Add an
appropriate initializer list and appropriate body
other.x_coordinate =
nullptr;
other.y_coordinate =
nullptr;
}
TwoDimensionalPoint
&TwoDimensionalPoint::operator=(const TwoDimensionalPoint
&rhs) {
// TODO: Add any
appropriate code
if (this !=
&rhs)
*x_coordinate = *rhs.x_coordinate;
*y_coordinate = *rhs.y_coordinate;
return *this;
}
TwoDimensionalPoint
&TwoDimensionalPoint::operator=(TwoDimensionalPoint
&&rhs) {
// TODO: Add any
appropriate code
std::swap(x_coordinate,
rhs.x_coordinate);
std::swap(y_coordinate,
rhs.y_coordinate);
return *this;
}
length_unit
TwoDimensionalPoint::DistanceFromOrigin() const {
// TODO: Calculate and
return the correct value
const coordinate_unit
temp_x = 0 - //no idea
const coordinate_unit
temp_y = 0 - //no idea
return sqrt( pow(temp_x,
2) + pow(temp_x, 2) );
}
length_unit
TwoDimensionalPoint::DistanceFromPoint(const Point &other)
const {
// TODO: Calculate and
return the correct value
const coordinate_unit
x_diff = other.GetX() - GetX();
const coordinate_unit
y_diff = other.GetY() - GetY();
return sqrt( pow(x_diff,
2) + pow(y_diff, 2) );
}
length_unit TwoDimensionalPoint::GetX() const
{
// TODO: Return the
appropriate value
const coordinate_unit
*x_coordinate = &x_coordinate;
return
*x_coordinate;
}
length_unit TwoDimensionalPoint::GetY() const
{
// TODO: Return the
appropriate value
const coordinate_unit
*y_coordinate = &y_coordinate;
return
*y_coordinate;
}
bool TwoDimensionalPoint::IsOrigin() const
{
// TODO Return the
correct value
if //unfinished due to
not knowing how to complete DistanceFromOrigin()
else
return false;
}
void
TwoDimensionalPoint::Translate(coordinate_unit x, coordinate_unit
y) {
// TODO: Implement
me
std::cout << "X is
translated by: " << x << std::endl;
std::cout << "Y is
translated by: " << y << std::endl;
} //This part confuses me as well since there
are different parameters. I.e., x and y instead of x_coordinate and
y_coordinate like throughout the rest of the program.
I feel like I'm close to finishing this, but Visual Studio Code has been throwing some problems throughout this, so I'm not completely certain. I've added some of my own comments to point out areas I'm particularly stuck. Any help would be highly appreciated!
// Point.h
#ifndef POINT_H
#define POINT_H
using length_unit = double;
using coordinate_unit = double;
/**
* A Point Interface.
*/
class Point {
public:
/**
* Calculate the distance this Point is from the origin.
* @return The distance this Point is from the origin is returned.
*/
virtual length_unit DistanceFromOrigin() const = 0;
/**
* Calculate the distance this Point is from the given Point.
* @param other another Point in the plane
* @return The distance this Point is from the given Point is returned.
*/
virtual length_unit DistanceFromPoint(const Point &other) const = 0;
virtual ~Point() = default;
};
#endif
//end of Point.h
// TwoDimensionalPoint.h
#ifndef TWODIMENSIONALPOINT_H
#define TWODIMENSIONALPOINT_H
#include "Point.h"
/**
* An implementation of the Point interface in a two-dimensional plane.
*/
class TwoDimensionalPoint : public Point {
private:
// TODO: Add appropriate data members
coordinate_unit *x_coordinate;
coordinate_unit *y_coordinate;
public:
/**
* Default/initializing constructor.
* @param x_coordinate the x-coordinate of this Point
* @param y_coordinate the y-coordinate of this Point
*/
explicit TwoDimensionalPoint(coordinate_unit x_coordinate = 0, coordinate_unit y_coordinate = 0);
/* The Big Five */
/**
* Destructor responsible for deleting memory occupied by the coordinates of this Point.
*/
~TwoDimensionalPoint() override;
/**
* Copy constructor used to create a new Point with the same coordinates of the other Point.
* @param other a Point used as a template for creating this point using copy semantics
*/
TwoDimensionalPoint(const TwoDimensionalPoint &other);
/**
* Move constructor used to create a new Point with the same coordinates of the other Point.
* @param other a Point used as a template for creating this point using move semantics
*/
TwoDimensionalPoint(TwoDimensionalPoint &&other) noexcept;
/**
* Copy assignment operator used to create a new Point with the same coordinates as the other Point.
* @param rhs an l-value; the right-hand side of the assignment statement lhs = rhs
* @return A new Point with the same coordinates as the given (l-value) TwoDimensionalPoint is returned.
*/
TwoDimensionalPoint &operator=(const TwoDimensionalPoint &rhs);
/**
* Move assignment operator used to create a new Point with the same coordinates as the other Point.
* @param rhs an r-value; the right-hand side of the assignment statement TwoDimensionalPoint lhs = TwoDimensionalPoint{x, y}
* @return A new Point with the same coordinates as the given (r-value) TwoDimensional Point is returned.
*/
TwoDimensionalPoint &operator=(TwoDimensionalPoint &&rhs);
/* Inherited Point behavior */
/**
* @copydoc Point::DistanceFromOrigin() const
*/
length_unit DistanceFromOrigin() const override;
/**
* @copydoc Point::DistanceFromPoint(const Point &other) const
*/
length_unit DistanceFromPoint(const Point &other) const override;
/* Specific TwoDimensionalPoint behavior */
/**
* X-coordinate accessor method.
* @return The x-coordinate of this Point is returned.
*/
virtual length_unit GetX() const;
/**
* Y-coordinate accessor method.
* @return The y-coordinate of this Point is returned.
*/
virtual length_unit GetY() const;
/**
* Assess whether this Point is the origin.
* @return True if this Point is the origin, otherwise false.
*/
virtual bool IsOrigin() const;
/**
* Translates this Point by the given dimensions.
* @param x the amount to translate in the x-direction
* @param y the amount to translate in the y-direction
*/
void Translate(coordinate_unit x = 0, coordinate_unit y = 0);
};
#endif
//end of TwoDimensionalPoint.h
// TwoDimensionalPoint.cpp
// #include <algorithm> // not required
#include <cmath>
#include <iostream>
#include "TwoDimensionalPoint.h"
TwoDimensionalPoint::TwoDimensionalPoint(coordinate_unit x_coordinate, coordinate_unit y_coordinate) : x_coordinate(&x_coordinate) , y_coordinate(&y_coordinate)
{
// TODO: Add an appropriate initializer list
}
TwoDimensionalPoint::~TwoDimensionalPoint() {
// TODO: Delete appropriate data members
delete x_coordinate;
delete y_coordinate;
}
TwoDimensionalPoint::TwoDimensionalPoint(const TwoDimensionalPoint &other) {
// TODO: Add an appropriate initializer list
x_coordinate = new coordinate_unit;
*x_coordinate = *other.x_coordinate;
y_coordinate = new coordinate_unit;
*y_coordinate = *other.y_coordinate;
}
TwoDimensionalPoint::TwoDimensionalPoint(TwoDimensionalPoint &&other) noexcept : x_coordinate(other.x_coordinate), y_coordinate(other.y_coordinate)
{
// TODO: Add an appropriate initializer list and appropriate body
other.x_coordinate = nullptr;
other.y_coordinate = nullptr;
}
TwoDimensionalPoint &TwoDimensionalPoint::operator=(const TwoDimensionalPoint &rhs) {
// TODO: Add any appropriate code
if (this != &rhs)
{
delete x_coordinate;
delete y_coordinate;
x_coordinate = new coordinate_unit;
*x_coordinate = *rhs.x_coordinate;
y_coordinate = new coordinate_unit;
*y_coordinate = *rhs.y_coordinate;
}
return *this;
}
TwoDimensionalPoint &TwoDimensionalPoint::operator=(TwoDimensionalPoint &&rhs) {
// TODO: Add any appropriate code
if(this != &rhs)
{
delete x_coordinate;
delete y_coordinate;
x_coordinate = rhs.x_coordinate;
y_coordinate = rhs.y_coordinate;
rhs.x_coordinate = nullptr;
rhs.y_coordinate = nullptr;
}
return *this;
}
length_unit TwoDimensionalPoint::DistanceFromOrigin() const {
// TODO: Calculate and return the correct value
// since coordinate of origin is (0,0)
const coordinate_unit temp_x = 0 - (*x_coordinate); //difference of x_coordinates
const coordinate_unit temp_y = 0 - (*y_coordinate); //difference of y_coordinates
return sqrt( pow(temp_x, 2) + pow(temp_y, 2) );
}
length_unit TwoDimensionalPoint::DistanceFromPoint(const Point &other) const {
// TODO: Calculate and return the correct value
// cast the Point to TwoDimensionalPoint class so that GetX() and GetY() are accessible
const TwoDimensionalPoint &other_point = dynamic_cast<const TwoDimensionalPoint&>(other);
const coordinate_unit x_diff = other_point.GetX() - GetX();
const coordinate_unit y_diff = other_point.GetY() - GetY();
return sqrt( pow(x_diff, 2) + pow(y_diff, 2) );
}
length_unit TwoDimensionalPoint::GetX() const {
// TODO: Return the appropriate value
return *x_coordinate;
}
length_unit TwoDimensionalPoint::GetY() const {
// TODO: Return the appropriate value
return *y_coordinate;
}
bool TwoDimensionalPoint::IsOrigin() const {
// TODO Return the correct value
const double EPSILON = 1e-5;
// since floating point cannot be compared using == , we check if absolute distance from origin < EPSILON, then its the origin
if(fabs(DistanceFromOrigin()) < EPSILON ) //unfinished due to not knowing how to complete DistanceFromOrigin()
return true;
else
return false;
}
void TwoDimensionalPoint::Translate(coordinate_unit x, coordinate_unit y) {
// TODO: Implement me
std::cout << "X is translated by: " << x << std::endl;
std::cout << "Y is translated by: " << y << std::endl;
*x_coordinate = (*x_coordinate) + x; // add x to x_coordinate
*y_coordinate = (*y_coordinate) + y; // add y to y_coordinate
}
//end of TwoDimensionalPoint.cpp