Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with designing a program for c++. Overload operator==, operator!= (def

ID: 3752358 • Letter: N

Question

Need help with designing a program for c++.

Overload operator==, operator!= (define in Point<T> only one equal() abstract function, override it in each subsequent class, use only this function in definition of operator== and operator!= 2. Since distanceTOrigin() function is defined, we can define operator> that is compare point objects by distance to origin from point.

3. Can create vector of pointers on Point objects. Why pointers? Because objects Point, Point2D, Point3D has different length, but pointers each has fixed length 4 bytes. So it is convenient to create vector of pointers on objects type Pointer.

4. Note, that Point object has no members so has length 0, Point2D object has 2 T type data members, Point3D has 3 T data members, T also can have different length depends of type.

5. Create function f(vector<Point<T>> &v) and pass there with vector<Point2D<T>> and vector<Point3D<T>> objects. (I did and passed there vector of Point3DWeighed<doube,double> objects)

6. Function f can be : given vector of points and given point, find point from the vector that is most close to given point – I implement in separate file as standalone function

7. Students need to find 2 points from the vector that are most close to each other.

8. I also implement operator+ to add 2 points; it  

9. Also, next child can be template WeightedPoint3D, child of Point3D template, added fourth variable weight, type W, so now, we have template WeightedPoint3D<T,W>

10. Overload functions: read, print (I did)

11. Distance to origin() is the same as in Point3D, multiply()is the same as in Point3D

12. Operator+ adds dimensions and adds weights

13. Does operator – make sense?

14. Operator< use abstract function compare()? In Point2D and in Point3D is measured by distance to origin and compare, in WeightedPoint3D it compared by impulse: distance*mass.  

15. Students can implement function distance between 2points assuming that points are given. Points can be Point2D, Point3D, and Weighted3D

**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 <iostream>

#include <fstream>

#include "Point3D.h"

#include "Point3DWeighed.h"

#include "VectorUtilities.h"

using namespace std;

namespace {

ifstream fin;

ofstream fout;

template<class V, class T, class P>

void printResult(

ostream &os,

const string &info,

const V &a,

const T &minDistance,

const P &minPoint

)

{

outputVector( os, a, info);

os <<" minDistance "<<minDistance<<", minPoint "<<minPoint<<" ";

}

template<class V, class T, class P>

void printResultOp(

ostream &os,

const string &info,

string &op,

V &a,

P &pointOp

)

{

outputVector( os, a, info);

os <<" pointOp "<<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,double> MyPoint;

double minDistance;

MyPoint minPoint;

vector<MyPoint*> 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<double> pp[5];

Point3D<double> 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<<i<<". "<<pp[i].getX()<<" "

<<pp[i].getY()<<" "

<<pp[i].getZ()<<" "

<<" ";

}

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<<i<<". "<<x<<" "<<y<<" "<<z<<" distance to origin = "

<<point[i].distanceToOrigin()<<" ";

}

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<double> 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<double,double> MyPoint;

MyPoint minPoint;

vector<MyPoint*> a;

string op="";

// MyPoint pointOp;

const string info = "testing operators";

fill2VectorsOp(fin, a, op);

if (op=="*")

{

fout<<*a[0]<<" ";

fout<<op<<" ";

fout<<*a[1]<<" is ";

fout<<(*a[0])*(*a[1])<<" ";

cout<<*a[0]<<" ";

cout<<op<<" ";

cout<<*a[1]<<" is ";

cout<<(*a[0])*(*a[1])<<" ";

}

fill2VectorsOp(fin, a, op);

if (op=="+")

{

fout<<*a[0]<<" ";

fout<<op<<" ";

fout<<*a[1]<<" is ";

fout<<*a[0]+*a[1]<<" ";

cout<<*a[0]<<" ";

cout<<op<<" ";

cout<<*a[1]<<" is ";

cout<<*a[0]+*a[1]<<" ";

}

fill2VectorsOp(fin, a, op);

if (op=="<")

{

fout<<*a[0]<<" ";

fout<<op<<" ";

fout<<*a[1]<<" is ";

if (*a[0] < *a[1])

{

fout<<" true ";

}

else

{

fout<<"false ";

}

}

fill2VectorsOp(fin, a, op);

if (op=="==")

{

fout<<*a[0]<<" ";

fout<<op<<" ";

fout<<*a[1]<<" is ";

if (*a[0]==*a[1])

{

fout<<" true ";

}

else

{

fout<<"false ";

}

}

fill2VectorsOp(fin, a, op);

if (op=="!=")

{

fout<<*a[0]<<" ";

fout<<op<<" ";

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 <iosfwd>

template <class T>

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

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

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

virtual bool less (const Point<T>& 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 <class U>

friend U operator*(const Point<U>& p1, const Point<U>& p2);

template <class U>

friend bool operator==(const Point<U>& p1, const Point<U>& p2);

// TODO Declare friend operator!= and operator<

template <class U>

friend std::ostream& operator<<(std::ostream& os, const Point<U>& point);

template <class U>

friend std::istream& operator>>(std::istream& is, Point<U>& point);

};

#include "Point-impl.h"

#endif /* POINT_H_ */

**point2D.h"

#ifndef POINT2D_H_

#define POINT2D_H_

#include <iosfwd> // include iostream declarations but not definitions

#include "Point.h"

template <class T>

class Point2D : public Point<T> {

public:

Point2D();

Point2D(const T x, const T y);

Point2D(const Point2D<T>& 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 <class U>

friend Point2D<U> operator+(const Point2D<U>& p1, const Point2D<U>& 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<T>& p1) const override;

virtual void add (const Point<T>& p1) override;

virtual bool equals (const Point<T>& p1) const override;

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

**point2D-impl.h**

/*

* Point2D-impl.h

*

* Created on: Jan 28, 2018

* Author: kamilla

*/

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

}

**point3D.h**

#ifndef POINT3D_H_

#define POINT3D_H_

#include <iosfwd> // include iostream declarations but not definitions

#include "Point2D.h"

template<class T>

class Point3D : public Point2D<T>

{

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 <class U>

friend Point3D<U> operator+(const Point3D<U>& p1, const Point3D<U>& p2);

protected:

T z;

virtual T multiply(const Point<T>& p1) const override;

virtual void add (const Point<T>& p1) override;

virtual bool equals (const Point<T>& p1) const override;

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

**point3D-impl.h**

#include "Point3D.h"

#include <cmath>

#include <iostream>

template <class T>

Point3D<T>::Point3D() : Point2D<T>(), 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 <class T>

Point3D<T>::Point3D(const T x1, const T y1, const T z1) :

Point2D<T>(x1, y1), z(z1)

{}

template <class T>

Point3D<T>::Point3D(const Point3D<T>& other) :

Point2D<T>(other), z(other.z)

{}

// TODO Define Point3D destructor

// TODO Define Point3D getZ method

// TODO Define Point3D getPoint method

template <class T>

void Point3D<T>::setZ(const T z1)

{

z=z1;

}

template <class T>

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

{

Point2D<T>::setPoint(x1,y1);

z=z1;

}

template <class T>

T Point3D<T>::distanceToOrigin() const

{

const T temp = Point2D<T>::distanceToOrigin();

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

}

template <class T>

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

{

// TODO Write multiply method body that uses

// Point2D multiply method appropriately

}

template <class T>

void Point3D<T>::add (const Point<T>& p1)

{

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

Point2D<T>::add(p1);

z += pp1.z;

}

template <class T>

bool Point3D<T>::equals (const Point<T>& p1) const

{

// TODO Write equals method body that uses

// Point2D equals method appropriately

}

template <class T>

bool Point3D<T>::less (const Point<T>& p1) const

{

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

return distanceToOrigin()<pp1.distanceToOrigin();

}

template <class T>

void Point3D<T>::print(std::ostream& os) const

{

Point2D<T>::print(os);

os << ", " << z;

// you can also write

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

}

template <class T>

void Point3D<T>::read (std::istream& is)

{

Point2D<T>::read(is);

std::string s;

is >> s >> z;

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

}

template <class T>

Point3D<T> operator+(const Point3D<T>& p1, const Point3D<T>& p2)

{

Point3D<T> temp(p1);

temp.add(p2);

return temp;

// return Point3D<T>(p1).add(p2);

}

**point3D-Weighed.h**

#ifndef POINT3DWEIGHED_H_

#define POINT3DWEIGHED_H_

// TODO include iostream declarations but not definitions

#include "Point3D.h"

template<class T, class W>

class Point3DWeighed : public Point3D<T>

{

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 <cmath>

#include <iostream>

#include "Point3DWeighed.h"

// TODO Define Point3DWeighed default constructor

template <class T, class W>

Point3DWeighed<T,W>::Point3DWeighed(const T x1, const T y1, const T z1, const W weight1) :

Point3D<T>(x1, y1, z1), weight(weight1)

{}

// TODO Define Point3DWeighed copy constructor

template <class T, class W>

Point3DWeighed<T,W>::~Point3DWeighed()

{}

template <class T,class W>

W Point3DWeighed<T,W>::getWeight() const

{

return weight;

}

template <class T, class W>

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

{

Point3D<T>::getPoint(x1,y1,z1);

weight1=weight;

}

// TODO Define Point3DWeighed setWeight method

// TODO Define Point3DWeighed setPoint method

template<class T, class W>

void Point3DWeighed<T,W>::add(const Point<T>& p1)

{

const Point3DWeighed<T,W> &pp1 = dynamic_cast<const Point3DWeighed<T,W>&>(p1);

Point3D<T>::add(p1);

weight += pp1.weight;

}

template <class T, class W>

bool Point3DWeighed<T,W>::equals (const Point<T>& p1) const

{

// TODO Write equals method body

}

template <class T, class W>

bool Point3DWeighed<T,W>::less (const Point<T>& p1) const

{

const Point3DWeighed<T,W> &pp1 = dynamic_cast<const Point3DWeighed<T,W>&>(p1);

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

}

template<class T, class W>

void Point3DWeighed<T,W>::print(std::ostream& os) const

{

// TODO Write print method body that

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

}

template<class T, class W>

void Point3DWeighed<T,W>::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 <class T, class W>

Point3DWeighed<T,W> operator+(const Point3DWeighed<T,W>& p1, const Point3DWeighed<T,W>& p2)

{

// TODO Write operator+ body

}

**Point-impl.h**

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

}

**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 <string>

#include <iosfwd>

#include <vector>

#include "Point.h"

template<class T>

void fillVector(

std::istream &is,

std::vector<Point<T>*> &a

);

template <class T>

void outputVector(

std::ostream &os,

const std::vector<Point<T>*> &a,

const std::string &info

);

template<class T>

void findPointMinDistanceToOrigin(

const std::vector<Point<T>*> &a,

T &distance,

Point<T> &p1

);

template<class T>

void fill2VectorsOp(

std::istream &is,

std::vector<Point<T>*> &a,

std::string &op

);

#include "VectorUtilities-impl.h"

#endif /* VECTORUTILITIES_H_ */

**VectorUtilities-impl.h**

#include <vector>

#include <iostream>

#include <cassert>

#include "VectorUtilities.h"

template <class P>

void fillVector(

std::istream &is,

std::vector<P*> &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<class P>

void outputVector(

std::ostream &os,

const std::vector<P*> &a,

const std::string &info

)

{

os<<info<<" [ ";

for (unsigned int i=0;i<a.size();++i)

{

os<<" "<<*a[i]<<" ";

}

os<<"] ";

}

template<class T,class P>

void findPointMinDistanceToOrigin(

const std::vector<P*> &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 )

{

minDistance = distance;

closestP = p;

start = false;

}

}

// closest P or default constructed P

p1 = closestP ? *closestP : P();

}

template<class P>

void fill2VectorsOp(

std::istream &is,

std::vector<P*> &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" );

}

Explanation / Answer

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,double> MyPoint;

double minDistance;

MyPoint minPoint;

vector<MyPoint*> 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 );

}

{

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

}

p1 = closestP ? *closestP : P();

}

template<class P>

void fill2VectorsOp(

std::istream &is,

std::vector<P*> &a,

std::string &op

)