Question: Need help with designing a program for c++. Overload operator==, operator!= (define in Point only one equal() abstract function, override it in each subsequent class,
Need help with designing a program for c++.
Overload operator==, operator!= (define in Point
**driver.cpp (below) doesnt need to be edited, just included it for clarity.**
**driver.cpp**
If the input file containes invalid data, you need to
* terminate the program immediately.
* To terminate the program write a throw statement
* like this
*
* throw runtime_error("Some error message");
*
* You will find out how to use throw statements later
* when learning about exceptions. For the moment, just
* assume that it terminates the program.
*/
#include
#include
#include "Point3D.h"
#include "Point3DWeighed.h"
#include "VectorUtilities.h"
using namespace std;
namespace {
ifstream fin;
ofstream fout;
template
void printResult(
ostream &os,
const string &info,
const V &a,
const T &minDistance,
const P &minPoint
)
{
outputVector( os, a, info);
os <<" minDistance "< } template void printResultOp( ostream &os, const string &info, string &op, V &a, P &pointOp ) { outputVector( os, a, info); os <<" pointOp "< } void testPoint3DWeighed() { cout<<" --------- testing PA3 Point3Weighed class " <<"and abstract class Point, Point2D, Point3d, Point3DWeighed " <<"standalone function findMinDistanceToOrigin() and create your " <<"own standalone finction findMinDistanceBetweenTwoPoints() "; // ifstream fin; // ofstream fout; // string fileNameI, fileNameO; typedef Point3DWeighed double minDistance; MyPoint minPoint; vector fillVector(fin, a); findPointMinDistanceToOrigin(a,minDistance,minPoint); const string info = "testing vector of Point3DWeighed"; printResult( fout, info, a, minDistance, minPoint ); printResult( cout, info, a, minDistance, minPoint ); } void testPoint3D() { cout<<" ----------- PA2 ch10-11 Class & Inheritnce part testing Point3D class "; Point3D Point3D double x,y,z; cout<<"A. Test operator>> and getX, getY, getZ "; for (int i=0;i<5;++i) { // Enter x, y and z from input file"; fin>>pp[i]; point[i].setPoint(pp[i].getX(),pp[i].getY(),pp[i].getZ()); } cout<<"B. Test pp getX, getY, getZ "; //here test getX() and getY() functions for (int i=0;i<5;++i) { cout< < < <<" "; } cout<<"C. Test point getPoint and distance to origin "; //here test getPoint() function for (int i=0;i<5;++i) { point[i].getPoint(x,y,z); cout< } cout<<"D. Test operator<< and distance to origin "; for (int i=0;i<5;++i) { cout << i << ". " << pp[i] << " distance to origin = " << pp[i].distanceToOrigin() << " "; } cout<<"E. Test operator<< and operator* (scalar multiplication) "; { double m = pp[0]*pp[1]; cout << pp[0] << " * " << pp[1] << " = " << m << " "; } cout<<"E. Test operator<< and operator+ "; { Point3D cout << pp[0] << " + " << pp[1] << " = " << ppp << " "; } } void testOperations() { cout<<" -------- testing Point3DWeighed operators*,+,<,==,!= "; cout<<"testing operator*, operator+, operator<, operator==,operator!= "; typedef Point3DWeighed MyPoint minPoint; vector string op=""; // MyPoint pointOp; const string info = "testing operators"; fill2VectorsOp(fin, a, op); if (op=="*") { fout<<*a[0]<<" "; fout< fout<<*a[1]<<" is "; fout<<(*a[0])*(*a[1])<<" "; cout<<*a[0]<<" "; cout< cout<<*a[1]<<" is "; cout<<(*a[0])*(*a[1])<<" "; } fill2VectorsOp(fin, a, op); if (op=="+") { fout<<*a[0]<<" "; fout< fout<<*a[1]<<" is "; fout<<*a[0]+*a[1]<<" "; cout<<*a[0]<<" "; cout< cout<<*a[1]<<" is "; cout<<*a[0]+*a[1]<<" "; } fill2VectorsOp(fin, a, op); if (op=="<") { fout<<*a[0]<<" "; fout< fout<<*a[1]<<" is "; if (*a[0] < *a[1]) { fout<<" true "; } else { fout<<"false "; } } fill2VectorsOp(fin, a, op); if (op=="==") { fout<<*a[0]<<" "; fout< fout<<*a[1]<<" is "; if (*a[0]==*a[1]) { fout<<" true "; } else { fout<<"false "; } } fill2VectorsOp(fin, a, op); if (op=="!=") { fout<<*a[0]<<" "; fout< fout<<*a[1]<<" is "; if (*a[0]!=*a[1]) { fout<<" true "; } else { fout<<"false "; } } } } int main() { try { string fileNameI, fileNameO; cout <<"Enter input fileNameI and output fileNameO: "; cin >> fileNameI >> fileNameO; fin.open(fileNameI.c_str()); fout.open(fileNameO.c_str()); testPoint3D(); testPoint3DWeighed(); testOperations(); fout.close(); fin.close(); } catch ( const exception &ex ) { cout << ex.what() << " "; } } **in_all1.txt** ( 3, 4, 1 ) ( 12, 1, 5 ) ( 1.1, 2.2, 3.3 ) ( 4.4, 5.5, 6.6 ) ( 7.7, 8.8, 9.9 ) [ ( 3.0, 0.0, 4.0, 90.0 ) ( 12.5, 1.0, 5.0, 61.0 ) ( 1.1, 2.2, 9.3, 22.3 ) ( 4.4, 5.5, 6.6, 71.5 ) ( 7.7, 8.8, 9.9, 34.4 ) ] [ ( 1.0, 2.0, 3.0, 90.0 ) * ( 0.5, 1.0, 5.0, 61.0 ) ] [ ( 3.0, 0.0, 4.0, 90.0 ) + ( 12.5, 1.0, 5.0, 61.0 ) ] [ ( 3.0, 0.0, 4.0, 90.0 ) < ( 12.5, 1.0, 5.0, 61.0 ) ] [ ( 3.0, 0.0, 4.0, 90.0 ) == ( 12.5, 1.0, 5.0, 61.0 ) ] [ ( 3.0, 0.0, 4.0, 90.0 ) != ( 12.5, 1.0, 5.0, 61.0 ) ] **out_all1.txt** testing vector of Point3DWeighed [ ( 3, 0, 4, 90 ) ( 12.5, 1, 5, 61 ) ( 1.1, 2.2, 9.3, 22.3 ) ( 4.4, 5.5, 6.6, 71.5 ) ( 7.7, 8.8, 9.9, 34.4 ) ] minDistance 5, minPoint ( 3, 0, 4, 90 ) ( 1, 2, 3, 90 ) * ( 0.5, 1, 5, 61 ) is 17.5 ( 3, 0, 4, 90 ) + ( 12.5, 1, 5, 61 ) is ( 15.5, 1, 9, 151 ) ( 3, 0, 4, 90 ) < ( 12.5, 1, 5, 61 ) is true ( 3, 0, 4, 90 ) == ( 12.5, 1, 5, 61 ) is false ( 3, 0, 4, 90 ) != ( 12.5, 1, 5, 61 ) is true **point.h** #ifndef POINT_H_ #define POINT_H_ #include template 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 virtual void add (const Point virtual bool equals (const Point virtual bool less (const Point 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 friend U operator*(const Point& p1, const Point& p2); template friend bool operator==(const Point& p1, const Point& p2); // TODO Declare friend operator!= and operator< template friend std::ostream& operator<<(std::ostream& os, const Point& point); template friend std::istream& operator>>(std::istream& is, Point& point); }; #include "Point-impl.h" #endif /* POINT_H_ */ **point2D.h" #ifndef POINT2D_H_ #define POINT2D_H_ #include #include "Point.h" template class Point2D : public Point public: Point2D(); Point2D(const T x, const T y); Point2D(const Point2D 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 friend Point2D operator+(const Point2D& p1, const Point2D& 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 virtual void add (const Point virtual bool equals (const Point virtual bool less (const Point virtual void print (std::ostream& os) const override; virtual void read (std::istream& is) override; }; #include "Point2D-impl.h" #endif /* POINT2D_H_ */ **point2D-impl.h** /* * Point2D-impl.h * * Created on: Jan 28, 2018 * Author: kamilla */ #include #include #include "Point2D.h" template Point2D {} template Point2D {} // TODO Define Point2D copy constructor template T Point2D { return x; } // TODO Define Point2D getY method template void Point2D { x1 = x; y1 = y; } template void Point2D { x = x1; } // TODO Define Point2D setY method template void Point2D { // TODO Write setPoint method body } template T Point2D { return std::sqrt(x*x+y*y); } template T Point2D { const Point2D return (x*pp1.x+y*pp1.y); } template void Point2D { const Point2D x += pp1.x; y += pp1.y; } template bool Point2D { // TODO Write equals method body // Be sure to check that p1 class is Point2D } template bool Point2D { return distanceToOrigin() } template void Point2D { std::string s; is >> x >> s >> y; if ( s != "," ) throw std::runtime_error( "Expected comma between x and y, got "+s); } template void Point2D { // TODO write print method body that prints x and y // separated by ", " } template Point2D {} template Point2D { Point2D temp.add(p2); return temp; // return Point2D } **point3D.h** #ifndef POINT3D_H_ #define POINT3D_H_ #include #include "Point2D.h" template class Point3D : public Point2D { 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 friend Point3D operator+(const Point3D& p1, const Point3D& p2); protected: T z; virtual T multiply(const Point virtual void add (const Point virtual bool equals (const Point virtual bool less (const Point virtual void print (std::ostream& os) const override; virtual void read (std::istream& is) override; }; #include "Point3D-impl.h" #endif /* POINT3D_H_ */ **point3D-impl.h** #include "Point3D.h" #include #include template Point3D { // 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 Point2D {} template Point3D Point2D {} // TODO Define Point3D destructor // TODO Define Point3D getZ method // TODO Define Point3D getPoint method template void Point3D { z=z1; } template void Point3D { Point2D z=z1; } template T Point3D { const T temp = Point2D return sqrt(temp*temp+z*z); } template T Point3D { // TODO Write multiply method body that uses // Point2D multiply method appropriately } template void Point3D { const Point3D Point2D z += pp1.z; } template bool Point3D { // TODO Write equals method body that uses // Point2D equals method appropriately } template bool Point3D { const Point3D return distanceToOrigin() } template void Point3D { Point2D os << ", " << z; // you can also write // os << *static_cast } template void Point3D { Point2D std::string s; is >> s >> z; if ( s != "," ) throw std::runtime_error( "Expected comma between y and z, got "+s); } template Point3D { Point3D temp.add(p2); return temp; // return Point3D } **point3D-Weighed.h** #ifndef POINT3DWEIGHED_H_ #define POINT3DWEIGHED_H_ // TODO include iostream declarations but not definitions #include "Point3D.h" template class Point3DWeighed : public Point3D { 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_ */ **point3Dweighed-impl.h** #include #include #include "Point3DWeighed.h" // TODO Define Point3DWeighed default constructor template Point3DWeighed Point3D {} // TODO Define Point3DWeighed copy constructor template Point3DWeighed {} template W Point3DWeighed { return weight; } template void Point3DWeighed { Point3D weight1=weight; } // TODO Define Point3DWeighed setWeight method // TODO Define Point3DWeighed setPoint method template void Point3DWeighed { const Point3DWeighed Point3D weight += pp1.weight; } template bool Point3DWeighed { // TODO Write equals method body } template bool Point3DWeighed { const Point3DWeighed return Point3D } template void Point3DWeighed { // TODO Write print method body that // prints base Point3D object, then ", ", and weight } template void Point3DWeighed { // 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 Point3DWeighed { // TODO Write operator+ body } **Point-impl.h** #include #include #include "Point.h" template T operator*(const Point { return p1.multiply(p2); } template bool operator==(const Point { return p1.equals(p2); } template bool operator!=(const Point { // 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 std::istream& operator>>(std::istream& is, 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 std::ostream& operator<<(std::ostream& os, const Point { os << "( "; point.print(os); os << " )"; return os; } **sample output** Enter input fileNameI and output fileNameO: in_all1.txt out_all1.txt ----------- PA2 ch10-11 Class & Inheritnce part testing Point3D class A. Test operator>> and getX, getY, getZ B. Test pp getX, getY, getZ 0. 3 4 1 1. 12 1 5 2. 1.1 2.2 3.3 3. 4.4 5.5 6.6 4. 7.7 8.8 9.9 C. Test point getPoint and distance to origin 0. 3 4 1 distance to origin = 5.09902 1. 12 1 5 distance to origin = 13.0384 2. 1.1 2.2 3.3 distance to origin = 4.11582 3. 4.4 5.5 6.6 distance to origin = 9.65246 4. 7.7 8.8 9.9 distance to origin = 15.3212 D. Test operator<< and distance to origin 0. ( 3, 4, 1 ) distance to origin = 5.09902 1. ( 12, 1, 5 ) distance to origin = 13.0384 2. ( 1.1, 2.2, 3.3 ) distance to origin = 4.11582 3. ( 4.4, 5.5, 6.6 ) distance to origin = 9.65246 4. ( 7.7, 8.8, 9.9 ) distance to origin = 15.3212 E. Test operator<< and operator* (scalar multiplication) ( 3, 4, 1 ) * ( 12, 1, 5 ) = 45 E. Test operator<< and operator+ ( 3, 4, 1 ) + ( 12, 1, 5 ) = ( 15, 5, 6 ) --------- testing PA3 Point3Weighed class and abstract class Point, Point2D, Point3d, Point3DWeighed standalone function findMinDistanceToOrigin() and create your own standalone finction findMinDistanceBetweenTwoPoints() testing vector of Point3DWeighed [ ( 3, 0, 4, 90 ) ( 12.5, 1, 5, 61 ) ( 1.1, 2.2, 9.3, 22.3 ) ( 4.4, 5.5, 6.6, 71.5 ) ( 7.7, 8.8, 9.9, 34.4 ) ] minDistance 5, minPoint ( 3, 0, 4, 90 ) -------- testing Point3DWeighed operators*,+,<,==,!= testing operator*, operator+, operator<, operator==,operator!= ( 1, 2, 3, 90 ) * ( 0.5, 1, 5, 61 ) is 17.5 ( 3, 0, 4, 90 ) + ( 12.5, 1, 5, 61 ) is ( 15.5, 1, 9, 151 ) **vectorUtilities.h** #ifndef VECTORUTILITIES_H_ #define VECTORUTILITIES_H_ #include #include #include #include "Point.h" template void fillVector( std::istream &is, std::vector ); template void outputVector( std::ostream &os, const std::vector const std::string &info ); template void findPointMinDistanceToOrigin( const std::vector T &distance, Point ); template void fill2VectorsOp( std::istream &is, std::vector std::string &op ); #include "VectorUtilities-impl.h" #endif /* VECTORUTILITIES_H_ */ **VectorUtilities-impl.h** #include #include #include #include "VectorUtilities.h" template void fillVector( std::istream &is, std::vector &a ) { std::string s; is >> s; if ( s != "[" ) throw std::runtime_error( "Expected [, got "+s); a.clear(); while (is) { char c = ' '; while ( std::isspace(c) ) is >> c; if ( c == ']' ) break; is.putback(c); P *temp = new P; is>>*temp; a.push_back(temp); } if ( !is ) throw std::runtime_error( "Invalid vector input format" ); } template void outputVector( std::ostream &os, const std::vector &a, const std::string &info ) { os< for (unsigned int i=0;i { os<<" "<<*a[i]<<" "; } os<<"] "; } template void findPointMinDistanceToOrigin( const std::vector &a, T &minDistance, P &p1 ) { bool start = true; P *closestP = nullptr; minDistance = T(); // zero initialization for ( const auto p : a ) { const auto distance = p->distanceToOrigin(); if ( start || distance { minDistance = distance; closestP = p; start = false; } } // closest P or default constructed P p1 = closestP ? *closestP : P(); } template void fill2VectorsOp( std::istream &is, std::vector &a, std::string &op ) { std::string s; is >> s; if ( s != "[" ) throw std::runtime_error( "Expected [, got "+s); a.clear(); // char c = ' '; // while ( std::isspace(c) ) is >> c; P *temp1 = new P; is>>*temp1; a.push_back(temp1); if ( !is ) throw std::runtime_error( "Invalid P input format" ); is>>op; P *temp2 = new P; is>>*temp2; if ( !is ) throw std::runtime_error( "Invalid P input format" ); a.push_back(temp2); is >> s; if ( s != "]" ) throw std::runtime_error( "Expected ], got "+s); if ( !is ) throw std::runtime_error( "Invalid vector input format" ); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
