Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming: Need help with the TODOs bolded: #include #include #include \"P

ID: 3756612 • Letter: C

Question

C++ Programming: Need help with the TODOs bolded:

#include

#include

#include "Point2D.h"

template

Point2D::Point2D() : x(0), y(0)

{}

template

Point2D::Point2D(const T x1, const T y1) : x(x1), y(y1)

{}

template

inline Point2D::Point2D(const Point2D& point)

{

}

// TODO Define Point2D copy constructor

template

T Point2D::getX() const

{

return x;

}

template

inline T Point2D::getY() const

{

return T();

}

// TODO Define Point2D getY method

template

void Point2D::getPoint(T& x1, T& y1) const

{

x1 = x;

y1 = y;

}

template

void Point2D::setX(const T x1)

{

x = x1;

}

template

inline void Point2D::setY(const T y)

{

}

// TODO Define Point2D setY method

template

void Point2D::setPoint(const T x1, const T y1)

{

// TODO Write setPoint method body

}

template

T Point2D::distanceToOrigin() const

{

return std::sqrt(x*x + y * y);

}

template

T Point2D::multiply(const Point& p1) const

{

const Point2D &pp1 = dynamic_cast&>(p1);

return (x*pp1.x + y * pp1.y);

}

template

void Point2D::add(const Point& p1)

{

const Point2D &pp1 = dynamic_cast&>(p1);

x += pp1.x;

y += pp1.y;

}

template

bool Point2D::equals(const Point& p1) const

{

// TODO Write equals method body

// Be sure to check that p1 class is Point2D

}

template

bool Point2D::less(const Point& p1) const

{

return distanceToOrigin()

}

template

void Point2D::read(std::istream& is)

{

std::string s;

is >> x >> s >> y;

if (s != ",") throw std::runtime_error("Expected comma between x and y, got " + s);

}

template

void Point2D::print(std::ostream& os) const

{

// TODO write print method body that prints x and y

// separated by ", "

}

template

Point2D::~Point2D()

{}

template

Point2D operator+(const Point2D& p1, const Point2D& p2)

{

Point2D temp(p1);

temp.add(p2);

return temp;

// return Point2D(p1).add(p2);

}

#pragma once

Point2D.h

#ifndef POINT2D_H_

#define POINT2D_H_

#include <iosfwd> // include iostream declarations but not definitions

#include "Point.h"

template <class T>

class Point2D : public Point<T> {

public:

Point2D();

Point2D(const T x, const T y);

Point2D(const Point2D<T>& point);

virtual ~Point2D() override;

T getX() const;

T getY() const;

void getPoint(T& x1, T& y1) const;

void setX(const T x);

void setY(const T y);

void setPoint(const T x1, const T y1);

// use override so compiler checks that the function is

// initially defined in the base class

virtual T distanceToOrigin() const override;

template <class U>

friend Point2D<U> operator+(const Point2D<U>& p1, const Point2D<U>& p2);

protected:

T x;

T y;

// use override so compiler checks that the function is

// initially defined in the base class

virtual T multiply(const Point<T>& p1) const override;

virtual void add(const Point<T>& p1) override;

virtual bool equals(const Point<T>& p1) const override;

virtual bool less(const Point<T>& p1) const override;

virtual void print(std::ostream& os) const override;

virtual void read(std::istream& is) override;

};

#include "Point2D-impl.h"

#endif /* POINT2D_H_ */

#pragma once

Point.h

#ifndef POINT_H_

#define POINT_H_

#include <iosfwd>

template <class T>

class Point {

public:

virtual ~Point() {}

// pure virtual functions must be defined in child classes

virtual T distanceToOrigin()   

const = 0;

protected:

// pure virtual functions must be defined in child classes

virtual T multiply(const Point<T>& p1) const = 0;

virtual void add(const Point<T>& p1) = 0;

virtual bool equals(const Point<T>& p1) const = 0;

virtual bool less(const Point<T>& p1) const = 0;

virtual void print(std::ostream& os) const = 0;

virtual void read(std::istream& is) = 0;

// treat friend functions declarations like they are written

// outside the class

template <class U>

friend U operator*(const Point<U>& p1, const Point<U>& p2);

template <class U>

friend bool operator==(const Point<U>& p1, const Point<U>& p2);

// TODO Declare friend operator!= and operator<

friend std::ostream& operator!=(std::ostream& os, const Point<U>& point)

friend std::ostream& operator<(std::ostream& os, const Point<U>& point)

template <class U>

friend std::ostream& operator<<(std::ostream& os, const Point<U>& point);

template <class U>

friend std::istream& operator>>(std::istream& is, Point<U>& point);

};

#include "Point-impl.h"

#endif /* POINT_H_ */

#pragma once

Explanation / Answer

Point.h

#ifndef POINT_H_

#define POINT_H_

#include <iosfwd>

template <class T>

class Point {

public:

virtual ~Point() {}

// pure virtual functions must be defined in child classes

virtual T distanceToOrigin()   

const = 0;

protected:

// pure virtual functions must be defined in child classes

virtual T multiply(const Point<T>& p1) const = 0;

virtual void add(const Point<T>& p1) = 0;

virtual bool equals(const Point<T>& p1) const = 0;

virtual bool less(const Point<T>& p1) const = 0;

virtual void print(std::ostream& os) const = 0;

virtual void read(std::istream& is) = 0;

// treat friend functions declarations like they are written

// outside the class

template <class U>

friend U operator*(const Point<U>& p1, const Point<U>& p2);

template <class U>

friend bool operator==(const Point<U>& p1, const Point<U>& p2);

// TODO Declare friend operator!= and operator<

template <class U>

friend bool operator!=(const Point<U>& p1,const Point<U>& p2);

template <class U>

friend bool operator<(const Point<U>& p1,const Point<U>& p2);

template <class U>

friend std::ostream& operator<<(std::ostream& os, const Point<U>& point);

template <class U>

friend std::istream& operator>>(std::istream& is, Point<U>& point);

};

#include "Point-impl.h"

#endif /* POINT_H_ */

#pragma once

Point2D.h

#ifndef POINT2D_H_

#define POINT2D_H_

#include <iosfwd> // include iostream declarations but not definitions

#include "Point.h"

template <class T>

class Point2D : public Point<T> {

public:

Point2D();

Point2D(const T x, const T y);

Point2D(const Point2D<T>& point);

virtual ~Point2D() override;

T getX() const;

T getY() const;

void getPoint(T& x1, T& y1) const;

void setX(const T x);

void setY(const T y);

void setPoint(const T x1, const T y1);

// use override so compiler checks that the function is

// initially defined in the base class

virtual T distanceToOrigin() const override;

template <class U>

friend Point2D<U> operator+(const Point2D<U>& p1, const Point2D<U>& p2);

protected:

T x;

T y;

// use override so compiler checks that the function is

// initially defined in the base class

virtual T multiply(const Point<T>& p1) const override;

virtual void add(const Point<T>& p1) override;

virtual bool equals(const Point<T>& p1) const override;

virtual bool less(const Point<T>& p1) const override;

virtual void print(std::ostream& os) const override;

virtual void read(std::istream& is) override;

};

#include "Point2D-impl.h"

#endif /* POINT2D_H_ */

#pragma once

Point2D.cpp

#include "Point2D.h"

#include <iostream>

#include <cmath>

template <class T>

Point2D<T>::Point2D() : x(0),y(0)

{}

template<class T>

Point2D<T>::Point2D(const T x1, const T y1) : x(x1), y(y1)

{}

// TODO Define Point2D copy constructor

template<class T>

Point2D<T>::Point2D(const Point2D<T>& point)

{

       x = point.x;

       y = point.y;

}

template<class T>

T Point2D<T>::getX() const

{

       return x;

}

// TODO Define Point2D getY method

template<class T>

T Point2D<T>::getY() const

{

       return y;

}

template<class T>

void Point2D<T>::getPoint(T& x1, T& y1) const

{

       x1 = x;

       y1 = y;

}

template<class T>

void Point2D<T>::setX(const T x1)

{

       x = x1;

}

// TODO Define Point2D setY method

template <class T>

void Point2D<T>::setY(const T y1)

{

       y = y1;

}

template<class T>

void Point2D<T>::setPoint(const T x1, const T y1)

{

// TODO Write setPoint method body

       x = x1;

       y = y1;

}

template <class T>

T Point2D<T>::distanceToOrigin() const

{

       return std::sqrt(x*x + y * y);

}

template<class T>

T Point2D<T>::multiply(const Point<T>& p1) const

{

       const Point2D<T> &pp1 = dynamic_cast<const Point2D<T>&>(p1);

       return (x*pp1.x + y * pp1.y);

}

template<class T>

void Point2D<T>::add(const Point<T>& p1)

{

       const Point2D<T> &pp1 = dynamic_cast<const Point2D<T>&>(p1);

       x += pp1.x;

       y += pp1.y;

}

template<class T>

bool Point2D<T>::equals(const Point<T>& p1) const

{

// TODO Write equals method body

// Be sure to check that p1 class is Point2D

       const Point2D<T> &pp1 = dynamic_cast<const Point2D<T>&>(p1);

       if (pp1 == NULL)

             return false;

       if(x == pp1.x && y == pp1.y)

             return true;

       return false;

}

template<class T>

bool Point2D<T>::less(const Point<T>& p1) const

{

       return distanceToOrigin()<p1.distanceToOrigin();

}

template<class T>

void Point2D<T>::read(std::istream& is)

{

       std::string s;

       is >> x >> s >> y;

       if (s != ",") throw std::runtime_error("Expected comma between x and y, got " + s);

}

template<class T>

void Point2D<T>::print(std::ostream& os) const

{

// TODO write print method body that prints x and y

// separated by ", "

       os<<x<<","<<y;

}

template<class T>

Point2D<T>::~Point2D()

{}

template<class T>

Point2D<T> operator+(const Point2D<T>& p1, const Point2D<T>& p2)

{

Point2D<T> temp(p1);

temp.add(p2);

return temp;

// return Point2D(p1).add(p2);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote