Help with this C++ code: Point3DWeighed.h #ifndef POINT3DWEIGHED_H_ #define POIN
ID: 3756915 • Letter: H
Question
Help with this C++ code:
Point3DWeighed.h
#ifndef POINT3DWEIGHED_H_
#define POINT3DWEIGHED_H_
// TODO include iostream declarations but not definitions
#include "Point3D.h"
template<class T, class W>
class Point3DWeighed : public Point3D<T>
{
public:
// TODO Declare default constructor
// TODO Declare constructor from x, y, z, w
// TODO Declare copy constructor
// TODO Declare destructor
W getWeight() const;
void getPoint(T& x1, T& y1, T& z1, W& weight1) const;
// TODO Declare matching setWeight
// TODO Declare matching setPoint
// virtual T distanceToOrigin() const override; from Point3D
// TODO Declare friend operator+
protected:
W weight;
// TODO Declare protected functions below.
// TODO add
// TODO equals
// TODO less
// TODO print
// TODO read
// TODO Use keywords virtual, const, and override appropriately
};
#include "Point3DWeighed-impl.h"
#endif /* POINT3DWEIGHED_H_ */
#pragma once
Point3DWeighed-impl.h
#include <cmath>
#include <iostream>
#include "Point3DWeighed.h"
// TODO Define Point3DWeighed default constructor
template <class T, class W>
Point3DWeighed<T, W>::Point3DWeighed(const T x1, const T y1, const T z1, const W weight1) :
Point3D<T>(x1, y1, z1), weight(weight1)
{}
// TODO Define Point3DWeighed copy constructor
template <class T, class W>
Point3DWeighed<T, W>::~Point3DWeighed()
{}
template <class T, class W>
W Point3DWeighed<T, W>::getWeight() const
{
return weight;
}
template <class T, class W>
void Point3DWeighed<T, W>::getPoint(T& x1, T& y1, T& z1, W& weight1) const
{
Point3D<T>::getPoint(x1, y1, z1);
weight1 = weight;
}
// TODO Define Point3DWeighed setWeight method
// TODO Define Point3DWeighed setPoint method
template<class T, class W>
void Point3DWeighed<T, W>::add(const Point<T>& p1)
{
const Point3DWeighed<T, W> &pp1 = dynamic_cast<const Point3DWeighed<T, W>&>(p1);
Point3D<T>::add(p1);
weight += pp1.weight;
}
template <class T, class W>
bool Point3DWeighed<T, W>::equals(const Point<T>& p1) const
{
// TODO Write equals method body
}
template <class T, class W>
bool Point3DWeighed<T, W>::less(const Point<T>& p1) const
{
const Point3DWeighed<T, W> &pp1 = dynamic_cast<const Point3DWeighed<T, W>&>(p1);
return Point3D<T>::distanceToOrigin()*weight < pp1.distanceToOrigin()*(pp1.weight);
}
template<class T, class W>
void Point3DWeighed<T, W>::print(std::ostream& os) const
{
// TODO Write print method body that
// prints base Point3D object, then ", ", and weight
}
template<class T, class W>
void Point3DWeighed<T, W>::read(std::istream& is)
{
// TODO Write read method body that
// reads base Point3D object, then ",", and weight
// throw exception if there is no ","
if (s != ",") throw std::runtime_error("Expected comma between z and w, got " + s);
}
template <class T, class W>
Point3DWeighed<T, W> operator+(const Point3DWeighed<T, W>& p1, const Point3DWeighed<T, W>& p2)
{
// TODO Write operator+ body
}
#pragma once
Here's the Point3D.h which might be needed for above code.
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 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
//Point3DWeighed.h
#ifndef POINT3DWEIGHED_H_
#define POINT3DWEIGHED_H_
// TODO include iostream declarations but not definitions
// TODO include iostream declarations but not definitions
#include <iosfwd>
#include "Point3D.h"
template<class T, class W>
class Point3DWeighed : public Point3D<T>
{
public:
// TODO Declare default constructor
Point3DWeighed();
// TODO Declare constructor from x, y, z, w
Point3DWeighed(T x, T y, T z,W w);
// TODO Declare copy constructor
Point3DWeighed(const Point3DWeighed& point);
// TODO Declare destructor
virtual ~Point3DWeighed() override;
W getWeight() const;
void getPoint(T& x1, T& y1, T& z1, W& weight1) const;
// TODO Declare matching setWeight
void setWeight(const W w);
// TODO Declare matching setPoint
void setPoint(const T x1, const T y1, const T z1);
// virtual T distanceToOrigin() const override; from Point3D
virtual T distanceToOrigin() const override;
// TODO Declare friend operator+
template <class U,class W1>
friend Point3DWeighed<U,W1> operator+(const Point3DWeighed<U,W1>& p1, const Point3DWeighed<U,W1>& p2);
protected:
W weight;
// TODO Declare protected functions below.
// TODO add
virtual void add(const Point<T>& p1) override;
// TODO equals
virtual bool equals(const Point<T>& p1) const override;
// TODO less
virtual bool less(const Point<T>& p1) const override;
// TODO print
virtual void print(std::ostream& os) const override;
// TODO read
virtual void read(std::istream& is) override;
// TODO Use keywords virtual, const, and override appropriately
};
#include "Point3DWeighed-impl.h"
#endif /* POINT3DWEIGHED_H_ */
#pragma once
// Point3DWeighed.cpp
#include <cmath>
#include <iostream>
#include "Point3DWeighed.h"
// TODO Define Point3DWeighed default constructor
template <class T,class W>
Point3DWeighed<T,W>::Point3DWeighed() : Point3D<T>{}, weight(0)
{}
template <class T, class W>
Point3DWeighed<T, W>::Point3DWeighed(const T x1, const T y1, const T z1, const W weight1) :
Point3D<T>(x1, y1, z1), weight(weight1)
{}
// TODO Define Point3DWeighed copy constructor
template <class T, class W>
Point3DWeighed<T,W>::Point3DWeighed(const Point3DWeighed<T,W> & other) : Point3D<T>(other), weight(other.weight)
{}
template <class T, class W>
Point3DWeighed<T, W>::~Point3DWeighed()
{}
template <class T, class W>
W Point3DWeighed<T, W>::getWeight() const
{
return weight;
}
template <class T, class W>
void Point3DWeighed<T, W>::getPoint(T& x1, T& y1, T& z1, W& weight1) const
{
Point3D<T>::getPoint(x1, y1, z1);
weight1 = weight;
}
// TODO Define Point3DWeighed setWeight method
template<class T,class W>
void Point3DWeighed<T,W>::setWeight(W w)
{
weight = w;
}
// TODO Define Point3DWeighed setPoint method
template<class T,class W>
void Point3DWeighed<T,W>::setPoint(const T x1, const T y1, const T z1)
{
Point3D<T>::setPoint(x1, y1,z1);
}
template<class T, class W>
void Point3DWeighed<T, W>::add(const Point<T>& p1)
{
const Point3DWeighed<T, W> &pp1 = dynamic_cast<const Point3DWeighed<T, W>&>(p1);
Point3D<T>::add(p1);
weight += pp1.weight;
}
template <class T, class W>
bool Point3DWeighed<T, W>::equals(const Point<T>& p1) const
{
const Point3DWeighed<T, W> &pp1 = dynamic_cast<const Point3DWeighed<T, W>&>(p1);
if(pp1 == NULL)
return false;
return(Point3D<T>::equals(p1) && (weight==pp1.weight));
}
template <class T, class W>
bool Point3DWeighed<T, W>::less(const Point<T>& p1) const
{
const Point3DWeighed<T, W> &pp1 = dynamic_cast<const Point3DWeighed<T, W>&>(p1);
return Point3D<T>::distanceToOrigin()*weight < pp1.distanceToOrigin()*(pp1.weight);
}
template<class T, class W>
void Point3DWeighed<T, W>::print(std::ostream& os) const
{
// TODO Write print method body that
// prints base Point3D object, then ", ", and weight
Point3D<T>::print(os);
os << ", " << weight;
}
template<class T, class W>
void Point3DWeighed<T, W>::read(std::istream& is)
{
// TODO Write read method body that
// reads base Point3D object, then ",", and weight
// throw exception if there is no ","
Point3D<T>::read(is);
std::string s;
is >> s >> weight;
if (s != ",") throw std::runtime_error("Expected comma between z and w, got " + s);
}
template <class T, class W>
Point3DWeighed<T, W> operator+(const Point3DWeighed<T, W>& p1, const Point3DWeighed<T, W>& p2)
{
Point3DWeighed<T,W> temp(p1);
temp.add(p2);
return temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.