ostream &operator <<(ostream &outs, const Point &p) { outs << "(" << p.getX() << ", " << p.getY() << ")"; // print out the value of the point as (x, y) return outs; }
#include "line.cpp" #include "point.cpp" void main() { Point xVal; Point yVal; xVal.setPoint(4.3,5.6); yVal.setPoint(6.8,3.9); xVal.getX(); yVal.getY(); xVal.printPolar(); yVal.printPolar(); <<(); }
//************************************************************************* // // Point Class definition file: point.h // //************************************************************************* #ifndef point_h #define point_h #include <iostream> using namespace std; class Point { private: double x, // define a point as (x,y) y; public: Point(); // default constructor Point(double xVal, double yVal); // argument constructor void setPoint(double xVAl, double yVal); // allow change to current point value double getX() const; // return the x value double getY() const; // return the y value void printPolar() const; // print the polar coordinate form of the point (x, y) }; ostream &operator <<(ostream &outs, const Point &p); // overload the insertion operator for point objects #endif
//*********************************************************************** // // Point implementation file: Point.cpp // //*********************************************************************** #include <iostream> #include <cmath> #include <iomanip> #include "point.h" // include the Point definition file using namespace std; const double PI = acos(-1); // define PI //****************************************************************** // // Constructor name: Point // // Purpose: creates a default Point object of (0,0) i.e. the origin // //******************************************************************* Point::Point() { x = y = 0.0; } //****************************************************************** // // Constructor name: Point // // Purpose: creates a Point object of value (xVal, yVal) // // Input parameters: double xVal, x coordinate // double yVal, y coordinate // //******************************************************************* Point::Point(double xVal, double yVal) { x = xVal; y = yVal; } //****************************************************************** // // Function name: setpoint // // Purpose: changes the x and y values of the point // to new values // // Input parameters: double xVal a new x value // double yVal a new y value // // Output parameters: none // // Return Value: void // //******************************************************************* void Point::setPoint(double xVal, double yVal) { x = xVal; y = yVal; } //****************************************************************** // // Function name: getX // // Purpose: returns the current x value of the point // // Input parameters: none // // Output parameters: none // // Return Value: double; the current value of x // //******************************************************************* double Point::getX() const { return x; } //****************************************************************** // // Function name: getY // // Purpose: returns the current y value of the point // // Input parameters: none // // Output parameters: none // // Return Value: double; the current value of y // //******************************************************************* double Point::getY() const { return y; } //****************************************************************** // // Function name: printPolar // // Purpose: prints to cout the value of the point in polar coordinates // // Input parameters: none // // Output parameters: none // // Return Value: void // //******************************************************************* void Point::printPolar() const { double r = sqrt(pow(x,2) + pow(y,2)), // calculate the distance of the point from the origin theta = atan(y / x); // calculate the angle of the point to the x-axis int ioFlags = cout.flags(); // save the cout flags for latter use cout << showpoint << fixed << setprecision(4); // set the cout object for 4 decimal places cout << r << '<' << theta; // print the radius and angle of the point use , for the angle symbol cout.flags(ioFlags); // restore the cout object to its original condition } //****************************************************************** // // Function name: << // // Purpose: prints to an ostream object the value of the point as (x, y) format // // Input parameters: outs and ostream object // p a point object // // Output parameters: outs an ostream object // // Return Value: a reference to an ostream object // //******************************************************************* ostream &operator <<(ostream &outs, const Point &p) { outs << "(" << p.getX() << ", " << p.getY() << ")"; // print out the value of the point as (x, y) return outs; }
//************************************************************************* // // Line Class definition file: line.h // //************************************************************************* #ifndef line_h #define line_h #include "point.h" //need the point definition file class Line { private: Point p1, // define a line as two points p2; double slope, // slope of the line if it exists y_intercept; // y intercept of the line if it exists bool vertical; // indicates if the line is vertical or not void calcSlpYInt(); // calculates the line slope and y intercept public: Line(); // default Line constructor Line(double x1, double y1, double x2, double y2); // Line constructor with double arguments for the two points Line(const Point &P1, const Point &P2); // Line constructor with point arguments void setLine(double x1, double y1, double x2, double y2); // change the values of the line points with doubles void setLine(const Point &P1, const Point &P2); // change the value of the line points with new point objects Point getP1() const; // return the point p1 of the line Point getP2() const; // return the point p2 of the line double getSlope() const; //return the slope value if it exists double getYIntercept() const; // return the y intercept if it exits bool vert() const; // returns a boolean value indicating if the line is vertical or not }; ostream &operator <<(ostream &outs, const Line &l); // overload the insertion operator for line objects #endif
//************************************************************************* // // Line Class implementation file: line.cpp // //************************************************************************* #include <cstdlib> #include "line.h" using namespace std; //****************************************************************** // // Function name: calcSlpYint // // Purpose: calculates the slope and y intercept of the line object // to new values // // Input parameters: none // // Output parameters: none // // Return Value: void // //******************************************************************* void Line::calcSlpYInt() { if ( abs(p1.getX() - p2.getX()) < 0.0001 ) // check for a vertical line vertical = true; // line is vertical else { // line is not vertical so calculate slope = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); // slope y_intercept = p2.getY() - slope * p2.getX(); // and y intercept vertical = false; } } //****************************************************************** // // Constructor name: Line // // Purpose: creates a default Line object containing the points (0,0) and (1,0) // i.e. a unit vector along the x-axis // //******************************************************************* Line::Line():p1(0,0), p2(1,0) { slope = y_intercept = 0; // for a horizontal line the slope and y intercept are 0 vertical = false; // it is not a vertical line } //****************************************************************** // // Constructor name: Line // // Purpose: creates a Line object with points (x1, y1) and (x2, y2) // // Input parameters: double x1, 1st x coordinate // double y1, 1st y coordinate // double x2, 2nd x coordinate // double y2, 2nd y coordinate // //****************************************************************** Line::Line(double x1, double y1, double x2, double y2):p1(x1, y1), p2(x2,y2) { calcSlpYInt(); } //****************************************************************** // // Constructor name: Line // // Purpose: creates a Line object with Point objects P1 and P2 // // Input parameters: P1, 1st point // P2, 2nd point // //****************************************************************** Line::Line(const Point &P1, const Point &P2):p1(P1.getX(), P1.getY()), p2(P2.getX(), P2.getY()) { calcSlpYInt(); } //****************************************************************** // // Function name: setLine // // Purpose: changes the point values of the line // to new values // // Input parameters: double x1, 1st x coordinate // double y1, 1st y coordinate // double x2, 2nd x coordinate // double y2, 2nd y coordinate // // Output parameters: none // // Return Value: void // //******************************************************************* void Line::setLine(double x1, double y1, double x2, double y2) { p1.setPoint(x1, y1); // change the value of point p1 p2.setPoint(x2, y2); // change the value of point p2 calcSlpYInt(); } //****************************************************************** // // Function name: setLine // // Purpose: changes the point values of the line // to new values // // Input parameters: P1 a point replacement for p1 // P2 a point replacement for p2 // // Output parameters: none // // Return Value: void // //******************************************************************* void Line::setLine(const Point &P1, const Point &P2) { p1 = P1; p2 = P2; calcSlpYInt(); } //****************************************************************** // // Function name: getP1 // // Purpose: returns the point object p1 // // Input parameters: none // // Output parameters: none // // Return Value: Point; the current value of p1 // //******************************************************************* Point Line::getP1() const { return p1; } //****************************************************************** // // Function name: getP2 // // Purpose: returns the point object p2 // // Input parameters: none // // Output parameters: none // // Return Value: Point; the current value of p2 // //******************************************************************* Point Line::getP2() const { return p2; } //****************************************************************** // // Function name: getSlope // // Purpose: returns the slope of the line // // Input parameters: none // // Output parameters: none // // Return Value: double the slope of the line // //******************************************************************* double Line::getSlope() const { return slope; } //****************************************************************** // // Function name: getYIntercept // // Purpose: returns the y intercept of the line // // Input parameters: none // // Output parameters: none // // Return Value: double the y intercept of the line // //******************************************************************* double Line::getYIntercept() const { return y_intercept; } //****************************************************************** // // Function name: vert // // Purpose: returns true if the line is vertical and false if the line is not vertical // // Input parameters: none // // Output parameters: none // // Return Value: bool the condition of the line in terms of being vertical // //******************************************************************* bool Line::vert() const { return vertical; } //****************************************************************** // // Function name: << // // Purpose: overloads the insertion operator to print out the equation of the line // // Input parameters: outs an ostream object // l a line object // // Output parameters: outs an ostream object // // Return Value: a reference to an ostream object // //******************************************************************* ostream &operator <<(ostream &outs, const Line &l) { int ioFlags = outs.flags(); // keep current state of ostream object if(l.vert()) // check if the line is vertical or not outs << "x = " << l.getP1().getX(); // it is vertical so print the x = equation else // not vertical so print the y=mx+b equation { outs << "y = " << l.getSlope() << "x"; if(abs(l.getYIntercept()) > 0.001) // only print the y intercept if it is non zero outs << showpos << l.getYIntercept(); // print the + sign if the y intercept is positive } outs.flags(ioFlags); // return ostream object to original state return outs; }
cout << xVal << ' ' << yVal << '\n';
| DaniWeb Message | |
| Cancel Changes | |