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

C++ : Need help with TODOs bolded part: #include \"Point3D.h\" #include #include

ID: 3756662 • Letter: C

Question

C++ : Need help with TODOs bolded part:

#include "Point3D.h"

#include

#include

template

Point3D::Point3D() : Point2D(), z(0)

{

// we use constructor call z(0) in the initialization list

// instead of assignment call

// z=0

// because, in general case, assignment is more expensive

// than constructor

}

template

Point3D::Point3D(const T x1, const T y1, const T z1) :

Point2D(x1, y1), z(z1)

{}

template

Point3D::Point3D(const Point3D& other) :

Point2D(other), z(other.z)

{}

// TODO Define Point3D destructor

// TODO Define Point3D getZ method

// TODO Define Point3D getPoint method

template

void Point3D::setZ(const T z1)

{

z = z1;

}

template

void Point3D::setPoint(const T x1, const T y1, const T z1)

{

Point2D::setPoint(x1, y1);

z = z1;

}

template

T Point3D::distanceToOrigin() const

{

const T temp = Point2D::distanceToOrigin();

return sqrt(temp*temp + z * z);

}

template

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

{

// TODO Write multiply method body that uses

// Point2D multiply method appropriately

}

template

void Point3D::add(const Point& p1)

{

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

Point2D::add(p1);

z += pp1.z;

}

template

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

{

// TODO Write equals method body that uses

// Point2D equals method appropriately

}

template

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

{

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

return distanceToOrigin()

}

template

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

{

Point2D::print(os);

os << ", " << z;

// you can also write

// os << *static_cast*>(this) << ", " << z;

}

template

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

{

Point2D::read(is);

std::string s;

is >> s >> z;

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

}

template

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

{

Point3D temp(p1);

temp.add(p2);

return temp;

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

}

#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);

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

Point 2D-impl.h

#include <cmath>

#include <iostream>

#include "Point2D.h"

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)

{}

template<class T>

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

{

}

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;

}

template<class T>

inline T Point2D<T>::getY() const

{

return T();

}

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;

}

template<class T>

inline void Point2D<T>::setY(const T y)

{

}

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)

{

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

{

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

{

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<T>(p1).add(p2);

}

#pragma once

Point 3D.h

#ifndef POINT3D_H_

#define POINT3D_H_

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

#include "Point2D.h"

template<class T>

class Point3D : public Point2D<T>

{

public:

Point3D();

Point3D(T x, T y, T z);

Point3D(const Point3D& point);

virtual ~Point3D() override;

T getZ() const;

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

void setZ(const T z);

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

virtual T distanceToOrigin() const override;

template <class U>

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

protected:

T z;

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 "Point3D-impl.h"

#endif /* POINT3D_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);

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

//Point 2D-impl.h

#include <cmath>

#include <iostream>

#include "Point2D.h"

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 y)

{

          this->y = y;

}

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);

}

//Point 3D.h

#ifndef POINT3D_H_

#define POINT3D_H_

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

#include "Point2D.h"

template<class T>

class Point3D : public Point2D<T>

{

public:

Point3D();

Point3D(T x, T y, T z);

Point3D(const Point3D& point);

virtual ~Point3D() override;

T getZ() const;

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

void setZ(const T z);

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

virtual T distanceToOrigin() const override;

template <class U>

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

protected:

T z;

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 "Point3D-impl.h"

#endif /* POINT3D_H_ */

#pragma once

// Point3D.cpp

#include "Point3D.h"

#include<iostream>

#include<cmath>

template<class T>

Point3D<T>::Point3D() : Point2D<T>{}, z(0)

{

// we use constructor call z(0) in the initialization list

// instead of assignment call

// z=0

// because, in general case, assignment is more expensive

// than constructor

}

template<class T>

Point3D<T>::Point3D(const T x1, const T y1, const T z1) :Point2D<T>(x1, y1), z(z1)

{}

template<class T>

Point3D<T>::Point3D(const Point3D& other) :Point2D<T>(other), z(other.z)

{}

// TODO Define Point3D destructor

template <class T>

Point3D<T>::~Point3D()

{}

// TODO Define Point3D getZ method

template <class T>

T Point3D<T>:: getZ() const

{

               return z;

}

// TODO Define Point3D getPoint method

template <class T>

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

{

               Point2D<T>::getPoint(x1,y1);

               z1 = z;

}

template<class T>

void Point3D<T>::setZ(const T z1)

{

               z = z1;

}

template<class T>

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

{

               Point2D<T>::setPoint(x1, y1);

               z = z1;

}

template<class T>

T Point3D<T>::distanceToOrigin() const

{

               const T temp = Point2D<T>::distanceToOrigin();

               return sqrt(temp*temp + z * z);

}

template<class T>

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

{

// TODO Write multiply method body that uses

// Point2D multiply method appropriately

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

               return (Point2D<T>::multiply(p1) + z*pp1.z );

}

template<class T>

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

{

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

               Point2D<T>::add(p1);

               z += pp1.z;

}

template<class T>

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

{

// TODO Write equals method body that uses

// Point2D equals method appropriately

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

               if(pp1 == NULL)

                              return false;

               return(Point2D<T>::equals(p1) && (z==pp1.z));

}

template<class T>

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

{

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

               return distanceToOrigin()<p1.distanceToOrigin();

}

template<class T>

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

{

               Point2D<T>::print(os);

               os << ", " << z;

// you can also write

// os << *static_cast*>(this) << ", " << z;

}

template<class T>

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

{

               Point2D<T>::read(is);

               std::string s;

               is >> s >> z;

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

}

template<class T>

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

{

               Point3D<T> temp(p1);

               temp.add(p2);

               return temp;

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

}