------------------------------------------------------------------------------
MC logo
C++ Point Class II
[^] Code Examples
------------------------------------------------------------------------------
<<C++ Point Class I cl1.cc Linked List (Structs)>>
#include <iostream>
#include <math.h>

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) {
                double xd = xval - other.xval;
                double yd = yval - other.yval;
                return sqrt(xd*xd + yd*yd);
        }

        // 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)
{
        p.print(strm);
        return strm;
}

main() 
{
        // Some points.
        Point a(5.2, -4.8);
        Point b(3.0, 9.0);
        Point c(-3.38);
        Point d;

        // Some arith. on the points.
        d = b - c;
        cout << b << " - " << c << " = " << d << endl;
        cout << a << " + " << b << " = " << (a + b) << endl;
        cout << a << " is " << a.dist(d) << " from " << d << endl;

        // Point variables are not references.
        Point fred[5];
        for(int m = 0; m < 5; m++) fred[m] = a;
        double w = 4.5;
        double x = -2.31;
        for(int m = 0; m < 5; m++) {
                fred[m].move(w, x);
                w += 3.4;
                x -= 1.3;
        }
        for(int m = 0; m < 5; m++) cout << fred[m] << endl;
}

This is the same as the previous, except operator overloading is used. Operator overloading allows you to assign meanings to existing operators on new types. In fact, the standard I/O libraries use this feature to support the << and >> I/O operators, as string uses it to make + concatenate. The present program overloads of << to make printing points much more convenient. The prline function used of the previous example is no longer needed. Overloading is only a convenience though: "syntactic sugar". We can get all the same things done without it.
<<C++ Point Class I Linked List (Structs)>>