#include <exception> #include <string> #include \"Point.h\" template <class T> T
ID: 3756672 • Letter: #
Question
#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
Need help with the TODOs bolded. Please
Explanation / Answer
In order to answer your the TODOs section correctly, you would need to provide me the implementation of Point class. This is because I need Point class methods to overload the '<' operator.
For time being, I am assuming that Point class has a less() method. With this assumption, I am answering your problem.
CODE
======================
#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);
}
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;
}
#pragma once
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.