Question: I need help finishing a program in C++ I have a point class that creates point objects and calculates the distance between points. I now
I need help finishing a program in C++
I have a point class that creates point objects and calculates the distance between points. I now need to add a line class that can take up to ten Point objects. I need to have exceptions added as well so if a user attempts to add more than ten points then the out-of-range exception is thrown. I need to add code to test for out-of-range conditions in the Main.cpp and catch the thrown exception.
Here is the code I have so far (please do not modify the Line.h and Point.h files:
Point.h #ifndef POINT_H_ #define POINT_H_ #include class Point { public: /** * Constructor and destructor */ Point(const double x, const double y); Point():x(0), y(0) {}; virtual ~Point(); /** * Get the x value */ double getX() const; /** * Get the y value */ double getY() const; /** * Return the distance between Points */ double distance(const Point &p) const; /** * Output the Point as (x, y) to an output stream */ friend std::ostream& operator <<(std::ostream&, const Point&); /** * Declare comparison relationships */ friend bool operator ==(const Point &lhs, const Point &rhs); friend bool operator <(const Point &lhs, const Point &rhs); /** * Declare math operators */ friend Point operator +(const Point &lhs, const Point &rhs); friend Point operator -(const Point &lhs, const Point &rhs); /** * Copy constructor */ Point(const Point & t); /** * Copy assignment */ Point& operator =( const Point& rhs ); private: double x; double y; }; #endif /* POINT_H_ */
Point.cpp #include "Point.h" #include using namespace std; //Constructor and destructor Point::Point(const double x, const double y){ this->x = x; this->y = y; } Point::~Point(){}; //Get the x value double Point::getX() const{ return x; } //Get the y value double Point::getY() const{ return y; } //Return the distance between Points double Point::distance(const Point &p) const{ double tempx, tempy; tempx = p.getX() - getX(); tempy = p.getY() - getY(); return sqrt((pow(tempx,2)+pow(tempy,2))); } //Output the Point as (x,y) to an output stream ostream& operator <<(ostream &out, const Point &point){ out << "(" << point.x << "," << point.y << ")"; return out; } //Declare comparison relationships bool operator ==(const Point &lhs, const Point &rhs){ return (lhs.x == rhs.x && lhs.y == rhs.y); } bool operator <(const Point &lhs, const Point &rhs){ return (lhs.x < rhs.x && lhs.y < rhs.y); } //Declare math operators Point operator +(const Point &lhs, const Point &rhs){ return Point(lhs.x + rhs.x, lhs.y + rhs.y); } Point operator -(const Point &lhs, const Point &rhs){ return Point(lhs.x - rhs.x, lhs.y - rhs.y); } //Copy constructor Point::Point(const Point & t){ x = t.x; y = t.y; } //Copy assignment Point& Point::operator =( const Point& rhs ){ if (this != &rhs){ Point tmp(rhs); } return *this; }
Line.h /* * Line.h * */ #ifndef LINE_H_ #define LINE_H_ #include "Point.h" class Line { public: /** * Constructor and destructor */ Line(); virtual ~Line(); /** * Add a point to the end of our line. If the line contains * ten points then throw an out_of_range exception. */ void push_back(const Point& p); /** * Clear the list of points */ void clear(); /** * Return the length of the line. The length is calculated as * the sum of the distance between all points in the line. */ double length(); private: unsigned int index; Point points[10]; }; #endif /* LINE_H_ */ Line.cpp #include "Line.h" #include "Point.h" #include using namespace std; //Constructor and destructor Line::~Line(){ index = 0; } //Add a point to end of line. If the line contains //ten points then throw an out_of_range exception. void Line::push_back(const Point& p){ points[index] = p; index++; } //Clear the list of points void Line::clear(){ index = 0; } //Return the length of the line. The length is calculated as //the sum of the distance between all points in the line. double Line::length(){ return 0; }
Main.cpp #include #include "Point.h" #include "Line.h" using namespace std; int main(){ double dist; Point tmp(0,0); //First set of points Point p1(1,3); Point p2(3,4); //Distance between Points dist = p1.distance(p2); cout << "The distance between p1 and p2 is: " << dist << endl; //Is one Point less than the other using points t1,t2,t3 and t4 if (p1 < p2){ cout << "p1 is less than p2" << endl; } //Are points equal using points p1,p2,p3 and p4 if (p1 == p2){ cout << "Points p1 and p2 are the same" << endl; } //Add Points tmp = p1 + p2; cout << "p1 added to p2 equals " << tmp << endl; //Subtract Points tmp = p1 - p2; cout << "p1 minus p2 equals " << tmp << endl; //Test copy constructor and copy assignment Point a(314,596); Point b = a; cout << "a.x = " << a.getX() << " a.y = " << a.getY() << endl; cout << "b.x = " << b.getX() << " b.y = " << b.getY() << endl; Line line[] = { p1, p2 }; cout << line; return 0; }