header
//*********************************************************
// GrossPay3a.cpp
//
// Calculate and display the gross pay given the hourly
// wage and the number of hours worked.
//
// This version illustrates a structure with a function
// component.
//
// D. Searls
// Asbury College
// Aug 2003
//*********************************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

const double MAXHOURS = 40.0;
const double OT_MULTIPLIER = 1.5;
const int MAXSIZE = 100;

struct employeeRecord
{
    string identity;
    double hours;
    double wageRate;
    
    //*********************************************************
    // grossPay
    //
    // Return the gross pay for the record that invokes this
    // function.
    //*********************************************************
    double grossPay() const
    {
        double pay;
        if (hours <= MAXHOURS)
        {
            pay = hours * wageRate;
        }
        else
        {
            pay = MAXHOURS * wageRate
                       + (hours - MAXHOURS) * OT_MULTIPLIER *wageRate;
        }
        return pay;
    }
};

//*********************************************************
// getEmployeeData
//
// Out Parameters: data, n
//
// Returns: void
//*********************************************************
void getEmployeeData(employeeRecord data[], int& n)
{
    ifstream infile;
    string identity;
    double wageRate;
    double hours;
    
    infile.open("EmployeeData.txt");
    n = 0;
    infile >> identity >> wageRate >> hours;
    while (!infile.fail() && n < MAXSIZE)
    {
        data[n].identity = identity;
        data[n].hours = hours;
        data[n].wageRate = wageRate;
        n = n+1;
        
        infile >> identity >> wageRate >> hours;
    }
    infile.close();
}

//*********************************************************
// displayGrossPay
//
// In Parameters: data, n
//
// Returns: void
//*********************************************************
void displayGrossPay(const employeeRecord data[], int n)
{
    cout << fixed << setprecision(2);
    cout << "Employee  Gross Pay" << endl;
    cout << "--------  ---------" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << setw(8) << data[i].identity
                << setw(11) << data[i].grossPay() << endl;
    }
}

//*********************************************************
//            M A I N   D R I V E R
//*********************************************************

int main()
{
    int n;              // Number of employees
    employeeRecord empData[MAXSIZE];

    getEmployeeData(empData, n);
    displayGrossPay(empData, n);
    
    return 0;
}

The structure declared in the program above can be replaced with the following class declaration with no changes required anywhere else in the program. Notice that there are only two small differences: the keyword "struct" is replaced by the keyword "class" and the keyword "public:" has been added to the code.

class employeeRecord
{
public:
    string identity;
    double hours;
    double wageRate;
    
    //*********************************************************
    // grossPay
    //
    // Return the gross pay for the record that invokes this
    // function.
    //*********************************************************
    double grossPay() const
    {
        double pay;
        if (hours <= MAXHOURS)
        {
            pay = hours * wageRate;
        }
        else
        {
            pay = MAXHOURS * wageRate
                       + (hours - MAXHOURS) * OT_MULTIPLIER *wageRate;
        }
        return pay;
    }
};