Point Test #3


Program Listings

PointTest3.cpp

/************************************************************************
*                                                                       *
*   Program:    PointTest3.cpp                                          *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include "point3.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.GetX() << ", " << P.GetY() << ")" << endl;
    cout << "P = (" << Q.GetX() << ", " << Q.GetY() << ")" << endl;
    cout << "P = (" << R.GetX() << ", " << R.GetY() << ")" << 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.GetX() << ", " << P.GetY() << ")" << 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.GetX() << ", " << P.GetY() << ")" << endl;

//  Test input

    cout << endl << "Test the input operator" << endl;
    cout << "Enter a point (x, y):  " << flush;
    char c;
    cin >> c >> x >> c >> y >> c;
    R.SetX(x);
    R.SetY(y);
    cout << "R = (" << R.GetX() << ", " << R.GetY() << ")" << endl;

//  Test for equality and inequality

//  Use equal points

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

//  Use unequal points

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

//  Test the addition and multiplication

    cout << endl << "Test the addition and multiplication operators" << endl;
    cout << "P + Q = ";             // Test addition of points
    R.SetX(P.GetX() + Q.GetX());
    R.SetY(P.GetY() + Q.GetY());
    cout << "(" << R.GetX() << ", " << R.GetY() << ")" << endl;
    
    cout << "5 * P = ";             // Test scalar multiplication
    R.SetX(5 * P.GetX());
    R.SetY(5 * P.GetY());
    cout << "(" << R.GetX() << ", " << R.GetY() << ")" << endl;
    
//  Test the distance function

    cout << endl << "Test the distance function" << endl;
    cout << "The distance from P to Q is ";     // Test the distance function
    cout << sqrt(pow(P.GetX() - Q.GetX(), 2) + pow(P.GetY() - Q.GetY(), 2)) << endl;
    
    cout << endl << "Good-bye" << endl;
    
    return 0;
}

point3.h

/************************************************************************
*                                                                       *
*   File:       point3.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, and the mutators                            *
*                                                                       *
************************************************************************/

#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

    private:

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

#endif

Point3.cpp

/************************************************************************
*                                                                       *
*   File:       Point.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, and the mutators                            *
*                                                                       *
************************************************************************/

//  Header files

#include "point3.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;
}

Sample Run

Test the constructors and the output operator
P = (0, 0)
P = (2, 3)
P = (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:46 1999 .

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

Return to Coms 261 home page