Compltete the areas labeled to do. #include <cmath> #include <iostream> #include
ID: 3755259 • Letter: C
Question
Compltete the areas labeled to do.
#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>
T Point2D<T>::getX() const
{
return x;
}
// TODO Define Point2D getY method
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>::setPoint(const T x1, const T y1)
{
// TODO Write setPoint method body
}
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
}
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 ", "
}
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);
}
Explanation / Answer
#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 & 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<T>(p1).add(p2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.