MC logo
Point Class Hdr File
[^] 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]
[Point Hdr File] [Point Impl File] [Point User File] [Point Class Hdr File] [Point Class Impl File] [Point Class User File] [C++ Integer Stack ADT] [C++ Integer Stack Implementation] [C++ Integer Stack Driver]
#include <iostream>
#include <math.h>

#ifndef _point4_h_
#define _point4_h_

using namespace std;

// Class to represent points.
class Point {
        double xval, yval;
        // 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(Point other) const;

        // 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 points using the << operator, so they can be printed along with
// other data types.
ostream & operator<<(ostream &strm, Point p);