Point Test #2


Program Listings

PointTest2.cpp

/************************************************************************
*                                                                       *
*   Program:    PointTest2.cpp                                          *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include "point2.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()
    
    cout << endl << "Good-bye" << endl;
    
    return 0;
}

point2.h

/************************************************************************
*                                                                       *
*   File:       point2h                                                 *
*                                                                       *
*   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 and the  *
*               inspectors                                              *
*                                                                       *
************************************************************************/

#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

    private:

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

#endif

Point2.cpp

/************************************************************************
*                                                                       *
*   File:       Point2.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 and the  *
*               inspectors                                              *
*                                                                       *
************************************************************************/

//  Header files

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

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

Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

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

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

Return to Coms 261 home page