------------------------------------------------------------------------------
MC logo
Point Class Header File III
[^] Code Examples
------------------------------------------------------------------------------
<<Point Class User File II point5.h Point Class Implementation File III>>
#include <iostream>

using namespace std;

// Class to represent points.
class Point {
private:
        double xval, yval;
public:
        // Constructor uses default arguments to allow calling with zero, one,
        // or two values.
        Point(double x = 0.0, double y = 0.0) {
                xval = x;
                yval = y;
        }

        // Extractors.
        double x() { return xval; }
        double y() { return yval; }

        // Distance to another point.  Pythagorean thm.
        double dist(Point other);

        // Add or subtract two points.
        Point operator+(Point b)
        {
                return Point(xval + b.xval, yval + b.yval);
        }
        Point operator-(Point b)
        {
                return Point(xval - b.xval, yval - b.yval);
        }

        // Move the existing point.
        void move(double a, double b)
        {
                xval += a;
                yval += b;
        }

        // Print the point on the stream.  The class ostream is a base class
        // for output streams of various types.
        void print(ostream &strm)
        {
                strm << "(" << xval << "," << yval << ")";
        }
};

// Print points using the << operator, so they can be printed along with
// other data types.
ostream & operator<<(ostream &strm, Point p);

Rounding off with the overloaded version. We also moved the printer function body to the implementation file. It can be left in the header, but then must be declared inline (or static works, but it's usually a bad idea).
<<Point Class User File II Point Class Implementation File III>>