Point Test #4


Program Listings

PointTest4.cpp

/************************************************************************
*                                                                       *
*   Program:    PointTest4.cpp                                          *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include "point4.h"

using namespace std;

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
************************************************************************/

int main() {

//  Test the constructors and the output operator

    cout << "Test the constructors and the output operator" << endl;
    point P;            // Test the default constructor
    point Q(2, 3);      // Test the other constructor
    point R = Q;        // Test the copy constructor
    
    cout << "P = ";
    P.Output(cout);     // Test the output facilitator
    cout << endl;
    cout << "Q = ";
    Q.Output(cout);
    cout << endl;
    cout << "R = ";
    R.Output(cout);
    cout << endl;

//  Test the assignment operator

    cout << endl << "Test the assignment operator" << endl;
    cout << "Assign Q to P:  ";
    P = Q;                          // Test the assignment operator
    cout << "P = ";
    P.Output(cout);
    cout << endl;
    
//  Test the inspectors

    cout << endl << "Test the inspectors" << endl;
    cout << "The x-coordinate of Q is " << Q.GetX() << endl;    // Test GetX()
    cout << "The y-coordinate of Q is " << Q.GetY() << endl;    // Test GetY()
    
//  Test the mutators

    cout << endl << "Test the mutators" << endl;
    cout << "Enter the x-coordinate of P:  " << flush;
    double x;
    cin >> x;
    P.SetX(x);                      // Test SetX()
    cout << "Enter the y-coordinate of P:  " << flush;
    double y;
    cin >> y;
    P.SetY(y);                      // Test SetY()
    cout << "P = ";
    P.Output(cout);
    cout << endl;

//  Test the input facilitator

    cout << endl << "Test the input operator" << endl;
    cout << "Enter a point (x, y):  " << flush;
    R.Input(cin);                   // Test the input facilitator
    cout << "R = ";
    R.Output(cout);
    cout << endl;

//  Test the equality facilitators

//  Use equal points

    cout << endl << "Test the equality operators" << endl;
    if (P.Equal(P))                 // Test equality of equals
        cout << "P == P" << endl;
    else
        cout << "P != P" << endl;
    
    if (!P.Equal(P))                // Test inequality of equals
        cout << "P != P" << endl;
    else
        cout << "P == P" << endl;

//  Use unequal points

    if (P.Equal(Q))                 // Test equality of unequals
        cout << "P == Q" << endl;
    else
        cout << "P != Q" << endl;
    
    if (!P.Equal(Q))                // Test inequality of unequals
        cout << "P != Q" << endl;
    else
        cout << "P == Q" << endl;

//  Test the add and multiply facilitator

    cout << endl << "Test the addition and multiplication operators" << endl;
    cout << "P + Q = ";             // Test addition of points
    P.Add(Q).Output(cout);
    cout << endl;
    cout << "5 * P = ";             // Test scalar multiplication
    P.Multiply(5).Output(cout);
    cout << endl;
    
//  Test the distance function

    cout << endl << "Test the distance function" << endl;
    cout << "The distance from P to Q is " << P.Distance(Q) << endl;
                                    // Test the distance function
    
    cout << endl << "Good-bye" << endl;
    
    return 0;
}

point4.h

/************************************************************************
*                                                                       *
*   File:       point4.h                                                *
*                                                                       *
*   Author:     Robb T. Koether                                         *
*                                                                       *
*   Date:       Oct 19, 1999                                            *
*                                                                       *
*   Abstract:   This file contains the definition of the point class    *
*               and related operators                                   *
*                                                                       *
*   Note:       This definition includes only the constructors, the     *
*               inspectors, the mutators, and the facilitators          *
*                                                                       *
************************************************************************/

#ifndef POINT_H
#define POINT_H

//  Header files

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

/************************************************************************
*                                                                       *
*   Definition of the point class                                       *
*                                                                       *
************************************************************************/

class point {

    public:

    //  Constructors

        point();                    // Default constructor
        point(double x, double y);  // Construct point from 2 floats

    //  Inspectors
    
        double GetX() const;        // Return the x-coordinate
        double GetY() const;        // Return the y-coordinate

    //  Mutators
    
        void SetX(double x);        // Set the x-coordinate
        void SetY(double y);        // Set the y-coordinate

    //  Facilitators
    
        void Input(istream& in);            // Extract this pt from istream
        void Output(ostream& out) const;    // Insert this pt into ostream
        bool Equal(const point& p) const;   // See if pt equals this pt
        point Add(const point& p) const;    // Add pt to this pt
        point Multiply(double s) const;     // Multiply this pt by scalar
    
    //  Other member functions
    
        double Distance(const point& p) const;  // Distance between points

    private:

        double xval;        // x-coordinate of the point
        double yval;        // y-coordinate of the point
};

#endif

Point4.cpp

/************************************************************************
*                                                                       *
*   File:       Point4.cpp                                              *
*                                                                       *
*   Author:     Robb T. Koether                                         *
*                                                                       *
*   Date:       Oct 19, 1999                                            *
*                                                                       *
*   Abstract:   This file implements the member functions and           *
*               operators of the point class                            *
*                                                                       *
*   Note:       This definition includes only the constructors, the     *
*               inspectors, the mutators, and the facilitators          *
*                                                                       *
************************************************************************/

//  Header files

#include "point4.h"
#include "assert.h"

/************************************************************************
*                                                                       *
*   CONSTRUCTORS                                                        *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   point()                                                 *
*                                                                       *
*   Purpose:    To construct the default point (0, 0)                   *
*                                                                       *
************************************************************************/

point::point() {
    xval = 0;
    yval = 0;
    return;
}

/************************************************************************
*                                                                       *
*   Function:   point(double, double)                                   *
*                                                                       *
*   Purpose:    To construct a point with the specified x- and          *
*               y-coordinates                                           *
*                                                                       *
************************************************************************/

point::point(double x, double y) {
    xval = x;
    yval = y;
    return;
}

/************************************************************************
*                                                                       *
*   INSPECTORS                                                          *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   GetX                                                    *
*                                                                       *
*   Purpose:    To return the x-coordinate of the point                 *
*                                                                       *
************************************************************************/

double point::GetX() const {
    return xval;
}

/************************************************************************
*                                                                       *
*   Function:   GetY                                                    *
*                                                                       *
*   Purpose:    To return the y-coordinate of the point                 *
*                                                                       *
************************************************************************/

double point::GetY() const {
    return yval;
}

/************************************************************************
*                                                                       *
*   MUTATORS                                                            *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   SetX                                                    *
*                                                                       *
*   Purpose:    To set the x-coordinate of the point                    *
*                                                                       *
************************************************************************/

void point::SetX(double x) {
    xval = x;
    return;
}

/************************************************************************
*                                                                       *
*   Function:   SetY                                                    *
*                                                                       *
*   Purpose:    To set the y-coordinate of the point                    *
*                                                                       *
************************************************************************/

void point::SetY(double y) {
    yval = y;
    return;
}

/************************************************************************
*                                                                       *
*   FACILITATORS                                                        *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   Input                                                   *
*                                                                       *
*   Purpose:    To extract a point from the specified input stream      *
*                                                                       *
************************************************************************/

void point::Input(istream& in) {
    char c;
    in >> c;            // Read open parenthesis
    assert(c == '(');   // Verify open parenthesis
    
    in >> xval;         // Read x-coordinate
    
    in >> c;            // Read comma
    assert(c == ',');   // Verify comma
    
    in >> yval;         // Read y-coordinate
    
    in >> c;            // Read close parenthesis
    assert(c == ')');   // Verify close parenthesis
    
    return;
}

/************************************************************************
*                                                                       *
*   Function:   Output                                                  *
*                                                                       *
*   Purpose:    To insert a point into the specified output stream      *
*                                                                       *
************************************************************************/

void point::Output(ostream& out) const {
    out << '(' << xval << ", " << yval << ')';
    return;
}

/************************************************************************
*                                                                       *
*   Function:   Equal                                                   *
*                                                                       *
*   Purpose:    To determine whether two points are equal               *
*                                                                       *
************************************************************************/

bool point::Equal(const point& p) const {
    return (xval == p.xval) && (yval == p.yval);
}

/************************************************************************
*                                                                       *
*   Function:   Add                                                     *
*                                                                       *
*   Purpose:    To add two points together coordinate-wise              *
*                                                                       *
************************************************************************/

point point::Add(const point& p) const {
    double x = xval + p.xval;
    double y = yval + p.yval;
    return point(x, y);
}

/************************************************************************
*                                                                       *
*   Function:   Multiply                                                *
*                                                                       *
*   Purpose:    To multiply a point by a scalar                         *
*                                                                       *
************************************************************************/

point point::Multiply(double s) const {
    double x = s * xval;
    double y = s * yval;
    return point(x, y);
}

/************************************************************************
*                                                                       *
*   OTHER MEMBER FUNCTIONS                                              *
*                                                                       *
************************************************************************/

/************************************************************************
*                                                                       *
*   Function:   Distance                                                *
*                                                                       *
*   Purpose:    To find the distance between two points                 *
*                                                                       *
************************************************************************/

double point::Distance(const point& p) const {
    return sqrt(pow(xval - p.xval, 2) + pow(yval - p.yval, 2));
}

Sample Run

Test the constructors and the output operator
P = (0, 0)
Q = (2, 3)
R = (2, 3)

Test the assignment operator
Assign Q to P:  P = (2, 3)

Test the inspectors
The x-coordinate of Q is 2
The y-coordinate of Q is 3

Test the mutators
Enter the x-coordinate of P:  4
Enter the y-coordinate of P:  8
P = (4, 8)

Test the input operator
Enter a point (x, y):  (6, 1)
R = (6, 1)

Test the equality operators
P == P
P == P
P != Q
P != Q

Test the addition and multiplication operators
P + Q = (6, 11)
5 * P = (20, 40)

Test the distance function
The distance from P to Q is 5.38516

Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:48 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/PointTest4.html

Return to Coms 261 home page