Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Trying to figure out this program. Need help with the sections labeled TODO in the following files. Completed files will be located after the others

Trying to figure out this program. Need help with the sections labeled TODO in the following files. Completed files will be located after the others and can assist with clarity. Thanks.

------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& p1) const = 0;

virtual void add (const Point& p1) = 0;

virtual bool equals (const Point& p1) const = 0;

virtual bool less (const Point& p1) const = 0;

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-impl.h-----

#include

#include

#include "Point2D.h"

template

Point2D::Point2D() : x(0), y(0)

{}

template

Point2D::Point2D(const T x1, const T y1) : x(x1), y(y1)

{}

// TODO Define Point2D copy constructor

template

T Point2D::getX() const

{

return x;

}

// TODO Define Point2D getY method

template

void Point2D::getPoint(T& x1,T& y1) const

{

x1 = x;

y1 = y;

}

template

void Point2D::setX(const T x1)

{

x = x1;

}

// TODO Define Point2D setY method

template

void Point2D::setPoint(const T x1, const T y1)

{

// TODO Write setPoint method body

}

template

T Point2D::distanceToOrigin() const

{

return std::sqrt(x*x+y*y);

}

template

T Point2D::multiply(const Point& p1) const

{

const Point2D &pp1 = dynamic_cast&>(p1);

return (x*pp1.x+y*pp1.y);

}

template

void Point2D::add (const Point& p1)

{

const Point2D &pp1 = dynamic_cast&>(p1);

x += pp1.x;

y += pp1.y;

}

template

bool Point2D::equals (const Point& p1) const

{

// TODO Write equals method body

// Be sure to check that p1 class is Point2D

}

template

bool Point2D::less (const Point& p1) const

{

return distanceToOrigin()

}

template

void Point2D::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

void Point2D::print(std::ostream& os) const

{

// TODO write print method body that prints x and y

// separated by ", "

}

template

Point2D::~Point2D()

{}

template

Point2D operator+(const Point2D& p1, const Point2D& p2)

{

Point2D temp(p1);

temp.add(p2);

return temp;

// return Point2D(p1).add(p2);

}

-----point3D-impl.h-----

#include "Point3D.h"

#include

#include

template

Point3D::Point3D() : Point2D(), z(0)

{

// 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::Point3D(const T x1, const T y1, const T z1) :

Point2D(x1, y1), z(z1)

{}

template

Point3D::Point3D(const Point3D& other) :

Point2D(other), z(other.z)

{}

// TODO Define Point3D destructor

// TODO Define Point3D getZ method

// TODO Define Point3D getPoint method

template

void Point3D::setZ(const T z1)

{

z=z1;

}

template

void Point3D::setPoint(const T x1, const T y1, const T z1)

{

Point2D::setPoint(x1,y1);

z=z1;

}

template

T Point3D::distanceToOrigin() const

{

const T temp = Point2D::distanceToOrigin();

return sqrt(temp*temp+z*z);

}

template

T Point3D::multiply(const Point& p1) const

{

// TODO Write multiply method body that uses

// Point2D multiply method appropriately

}

template

void Point3D::add (const Point& p1)

{

const Point3D &pp1 = dynamic_cast&>(p1);

Point2D::add(p1);

z += pp1.z;

}

template

bool Point3D::equals (const Point& p1) const

{

// TODO Write equals method body that uses

// Point2D equals method appropriately

}

template

bool Point3D::less (const Point& p1) const

{

const Point3D &pp1 = dynamic_cast&>(p1);

return distanceToOrigin()

}

template

void Point3D::print(std::ostream& os) const

{

Point2D::print(os);

os << ", " << z;

// you can also write

// os << *static_cast*>(this) << ", " << z;

}

template

void Point3D::read (std::istream& is)

{

Point2D::read(is);

std::string s;

is >> s >> z;

if ( s != "," ) throw std::runtime_error( "Expected comma between y and z, got "+s);

}

template

Point3D operator+(const Point3D& p1, const Point3D& p2)

{

Point3D temp(p1);

temp.add(p2);

return temp;

// return Point3D(p1).add(p2);

}

-----point3Dweighed.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::Point3DWeighed(const T x1, const T y1, const T z1, const W weight1) :

Point3D(x1, y1, z1), weight(weight1)

{}

// TODO Define Point3DWeighed copy constructor

template

Point3DWeighed::~Point3DWeighed()

{}

template

W Point3DWeighed::getWeight() const

{

return weight;

}

template

void Point3DWeighed::getPoint(T& x1, T& y1, T& z1, W& weight1) const

{

Point3D::getPoint(x1,y1,z1);

weight1=weight;

}

// TODO Define Point3DWeighed setWeight method

// TODO Define Point3DWeighed setPoint method

template

void Point3DWeighed::add(const Point& p1)

{

const Point3DWeighed &pp1 = dynamic_cast&>(p1);

Point3D::add(p1);

weight += pp1.weight;

}

template

bool Point3DWeighed::equals (const Point& p1) const

{

// TODO Write equals method body

}

template

bool Point3DWeighed::less (const Point& p1) const

{

const Point3DWeighed &pp1 = dynamic_cast&>(p1);

return Point3D::distanceToOrigin()*weight < pp1.distanceToOrigin()*(pp1.weight);

}

template

void Point3DWeighed::print(std::ostream& os) const

{

// TODO Write print method body that

// prints base Point3D object, then ", ", and weight

}

template

void Point3DWeighed::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

Point3DWeighed operator+(const Point3DWeighed& p1, const Point3DWeighed& p2)

{

// TODO Write operator+ body

}

-----point-impl.h-----

#include

#include

#include "Point.h"

template

T operator*(const Point& p1, const Point& p2)

{

return p1.multiply(p2);

}

template

bool operator==(const Point& p1, const Point& p2)

{

return p1.equals(p2);

}

template

bool operator!=(const Point& p1, const Point& 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

std::istream& operator>>(std::istream& is, Point& 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& point)

{

os << "( ";

point.print(os);

os << " )";

return os;

}

***************************************finished files*********************************************

-----driver.cpp-----

#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 MyPoint;

double minDistance;

MyPoint minPoint;

vector a;

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 pp[5];

Point3D point[5];

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 ppp = pp[0]+pp[1];

cout << pp[0]

<< " + "

<< pp[1]

<< " = "

<< ppp

<< " ";

}

}

void testOperations()

{

cout<<" -------- testing Point3DWeighed operators*,+,<,==,!= ";

cout<<"testing operator*, operator+, operator<, operator==,operator!= ";

typedef Point3DWeighed MyPoint;

MyPoint minPoint;

vector a;

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

-----point2D.h-----

#ifndef POINT2D_H_

#define POINT2D_H_

#include // include iostream declarations but not definitions

#include "Point.h"

template

class Point2D : public Point {

public:

Point2D();

Point2D(const T x, const T y);

Point2D(const Point2D& point);

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& p1) const override;

virtual void add (const Point& p1) override;

virtual bool equals (const Point& p1) const override;

virtual bool less (const Point& p1) const override;

virtual void print (std::ostream& os) const override;

virtual void read (std::istream& is) override;

};

#include "Point2D-impl.h"

#endif /* POINT2D_H_ */

-----point3D.h-----

#ifndef POINT3D_H_

#define POINT3D_H_

#include // include iostream declarations but not definitions

#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& p1) const override;

virtual void add (const Point& p1) override;

virtual bool equals (const Point& p1) const override;

virtual bool less (const Point& 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_ */

-----vectorutilities.h-----

#ifndef VECTORUTILITIES_H_

#define VECTORUTILITIES_H_

#include

#include

#include

#include "Point.h"

template

void fillVector(

std::istream &is,

std::vector*> &a

);

template

void outputVector(

std::ostream &os,

const std::vector*> &a,

const std::string &info

);

template

void findPointMinDistanceToOrigin(

const std::vector*> &a,

T &distance,

Point &p1

);

template

void fill2VectorsOp(

std::istream &is,

std::vector*> &a,

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" );

}

********************************** Output were supposed to get after everything****************************************

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 )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions