#include #include #include using namespace std; #ifndef _point_h_ #define _point_h_ /* * Class to represent a single point. */ class Point { public: // Construct a point with the indicated coordinates. Note // use fo a constructor list instead of assignments in the body. Point(double x = 0.0, double y = 0.0): m_x(x), m_y(y) { } // Return either ordinate. double abscissa() const { return m_x; } double ordinate() const { return m_y; } // Print a string on the indicated stream. cout is type of // std::ostream (descendent class, actually) void print(std::ostream &strm) const { strm << "(" << m_x << "," << m_y << ")"; } // Add or subtract two points Point plus(const Point &b) const { return Point(m_x + b.m_x, m_y + b.m_y); } Point minus(const Point &b) const { return Point(m_x - b.m_x, m_y - b.m_y); } // Negate the point Point negate() const { return Point(-m_x, -m_y); } // Move the point by a given amount void move(double dx, double dy) { m_x += dx; m_y += dy; } // Return the distance between points double distance(const Point &b) const { double xd = m_x - b.m_x; double yd = m_y - b.m_y; return sqrt(xd*xd + yd*yd); } // Return the area of the rectangle whose corners are the // current point and the argument. double area(const Point &b) const { return abs(m_x - b.m_x) * abs(m_y - b.m_y); } private: // The coordinates of the point. double m_x, m_y; }; /* * Print the point. When you have Point p, and you say * cout << p * the compiler translates it to * p.operator<<(cout) */ static ostream & operator<<(std::ostream &strm, const Point &p) { p.print(strm); return strm; } #endif