// A simple example of a class representing points in the X, Y plane. // // Mark A. Sheldon // Tufts University // Sprint 2013 #include #include using namespace std; /////////////////////////////////////////////////////////////////////////// // Point Class Interface // /////////////////////////////////////////////////////////////////////////// class Point { public: Point(int x_coord, int y_coord); int get_x(); int get_y(); Point add(Point other); void print(); private: int x, y; }; /////////////////////////////////////////////////////////////////////////// // Point Class Client // /////////////////////////////////////////////////////////////////////////// int main() { Point p1(-1, 2), p2(3, 4); Point p3 = p1.add(p2); cout << "p3: "; p3.print(); cout << endl; } /////////////////////////////////////////////////////////////////////////// // Implementation of Points // /////////////////////////////////////////////////////////////////////////// // // Initialize a new point with given coordinates // Point::Point(int x_coord, int y_coord) { x = x_coord; y = y_coord; } // // Print out a point // void Point::print() { cout << "(" << x << ", " << y << ")"; } // // return the x-coordinate of the point // int Point::get_x() { return x; } // // return the y-coordinate of the point // int Point::get_y() { return y; } // // return a point whose coordinates are the sum of the coordinates // of this point and another one // Point Point::add(Point other) { Point result(get_x() + other.get_x(), get_y() + other.get_y()); return result; }