Question

In: Computer Science

(C++) How do I go about obtaining the origin of a point via an instance variable?...

(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!

Solutions

Expert Solution

// 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


Related Solutions

Correction to earlier question: Via the Born Oppenheimer Approximation, How do you go about solving for...
Correction to earlier question: Via the Born Oppenheimer Approximation, How do you go about solving for the wave function of H2+ and H2, once you've constructed the electronic schrodinger eq?
How do I go about showing in an IS - LM - IP diagram a decrease...
How do I go about showing in an IS - LM - IP diagram a decrease of interest rates but an increase in the exchange rate?  
How does on go about solving the schrodinger equation directly (not via perturbation theory) for a...
How does on go about solving the schrodinger equation directly (not via perturbation theory) for a infinite square well (from 0 to L) with a delta function potential in the middle or along some fraction of the well? In th two separate regions the solution i simple. how do you deal with the delta potential in the middle?
How do you do a 90 degree counter-clockwise rotation around a point? I know around the origin it's (−y,x), but what would it be around a point?
How do you do a 90 degree counter-clockwise rotation around a point? I know around the origin it's (−y,x), but what would it be around a point?
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i =...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i = 0; for(j = 0; j < n - 1; j++) int iMin = j; for(i = j + 1; i < n; i++) if(a[i] < a[iMin]) iMin = i; if(iMin != j) swap(a[i], a[iMin]);
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
How far can you go with transformations? For instance, can I transform data using square root,...
How far can you go with transformations? For instance, can I transform data using square root, arcsin, and then natural log? If I shouldn’t, why not?
What is a sample? How do yo go about selecting one?
What is a sample? How do yo go about selecting one?
A positive point charge Q1 = 1.8 ? 10-5 C is fixed at the origin of...
A positive point charge Q1 = 1.8 ? 10-5 C is fixed at the origin of coordinates, and a negative charge Q2 = -3.6 ? 10-6 C is fixed to the x axis at x = +2.0 m. Find the location of the place(s) along the x axis where the electric field due to these two charges is zero. (Enter the locations in order from -x to +x. If there is only 1 location, enter 0 as the second answer.)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT