Point.h https://drive.google.com/file/d/1Mly4VFfPxXWUW-qPA1WDESMwqdo8ZjIC/view N
ID: 3757254 • Letter: P
Question
Point.h
https://drive.google.com/file/d/1Mly4VFfPxXWUW-qPA1WDESMwqdo8ZjIC/view
Need help with the TODOs bolded. Please
#include <exception>
#include <string>
#include "Point.h"
template <class T>
T operator*(const Point<T>& p1, const Point<T>& p2)
{
return p1.multiply(p2);
}
template <class T>
bool operator==(const Point<T>& p1, const Point<T>& p2)
{
return p1.equals(p2);
}
template <class T>
bool operator!=(const Point<T>& p1, const Point<T>& p2)
{
// TODO Write operator!= body that calls equals method
}
// TODO Define operator< that calls less method to compare Points
/* Read Point like this
* ( 1, 2 )
* ( 3, 4, 5 )
* etc.
* Text inside ( ) is read via child's virtual read() method.
* Throw exceptions if ( ) are missing or if istream fails
*/
template <class T>
std::istream& operator>>(std::istream& is, Point<T>& point)
{
std::string s;
is >> s;
if (s != "(") throw std::runtime_error("Expected (, got " + s);
point.read(is);
if (!is) throw std::runtime_error("Invalid input format");
char c = ' ';
while (std::isspace(c)) is >> c;
if (c != ')') throw std::runtime_error("Expected ), got " + s);
return is;
}
/* Print Point like this
* ( 1, 2 )
* ( 3, 4, 5 )
* etc.
* Text inside ( ) is printed via child's virtual print() method.
*/
template <class T>
std::ostream& operator<<(std::ostream& os, const Point<T>& point)
{
os << "( ";
point.print(os);
os << " )";
return os;
}
#pragma once
Explanation / Answer
//Point.cpp
#include <exception>
#include <string>
#include "Point.h"
template <class T>
T operator*(const Point<T>& p1, const Point<T>& p2)
{
return p1.multiply(p2);
}
template <class T>
bool operator==(const Point<T>& p1, const Point<T>& p2)
{
return p1.equals(p2);
}
template <class T>
bool operator!=(const Point<T>& p1, const Point<T>& p2)
{
return (!p1.equals(p2));
// TODO Write operator!= body that calls equals method
}
// TODO Define operator< that calls less method to compare Points
template <class T>
bool operator<(const Point<T>& p1,const Point<T>& p2)
{
return p1.less(p2);
}
/* Read Point like this
* ( 1, 2 )
* ( 3, 4, 5 )
* etc.
* Text inside ( ) is read via child's virtual read() method.
* Throw exceptions if ( ) are missing or if istream fails
*/
template <class T>
std::istream& operator>>(std::istream& is, Point<T>& point)
{
std::string s;
is >> s;
if (s != "(") throw std::runtime_error("Expected (, got " + s);
point.read(is);
if (!is ) throw std::runtime_error("Invalid input format");
char c = ' ';
while (std::isspace(c)) is >> c;
if (c != ')') throw std::runtime_error("Expected ), got " + s);
return is;
}
/* Print Point like this
* ( 1, 2 )
* ( 3, 4, 5 )
* etc.
* Text inside ( ) is printed via child's virtual print() method.
*/
template <class T>
std::ostream& operator<<(std::ostream& os, const Point<T>& point)
{
os << "( ";
point.print(os);
os << " )";
return os;
}