------------------------------------------------------------------------------
MC logo
C++ Point Class
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Plain Structs] [Point With Struct] [C++ Point Class] [Linked List (Structs)] [Linked List (Classes)] [C++ v. Java Classes I] [Prime Generator Algorithm] [Fast Primes Generator]
cl0.cc
#include <iostream>
#include <string>
#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() const { return xval; }
        double y() const { return yval; }

        // Distance to another point.  Pythagorean thm.
        double dist(const Point & other) const {
                double xd = xval - other.xval;
                double yd = yval - other.yval;
                return sqrt(xd*xd + yd*yd);
        }

        // Add or subtract two points.
        Point add(Point b) const
        {
                return Point(xval + b.xval, yval + b.yval);
        }
        Point sub(Point b) const
        {
                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) const
        {
                strm << "(" << xval << "," << yval << ")";
        }
};

// Print a line of the form x op y = z, where x, y, and z are points. 
void prline(ostream &strm, const Point &x, string op, const Point &y, 
            const Point & z)
{
        x.print(strm);
        cout << " " << op << " ";
        y.print(strm);
        cout << " = ";
        z.print(strm);
        cout << endl;
}

// Print points using the << operator, so they can be printed along with
// other data types.
ostream & operator<<(ostream &strm, const 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.sub(c);
        prline(cout, b, "-", c, d);
        prline(cout, a, "+", b, a.add(b));
        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 program declares and uses a simple point class. Some things to note: