﻿ Array of Pointers
```//*********************************************************
// GrossPay10.cpp
//
// Calculate and display the gross pay given the hourly
// wage and the number of hours worked.
//
// This version uses an array of pointers to employee
// structures.
//
// D. Searls
// Asbury College
// Dec 2009
//*********************************************************
#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;
double grossPay;
};

void getEmployeeData(employeeRecord*[], int&);
void calculateGrossPay(employeeRecord*[], int);
void displayGrossPay(employeeRecord*[], int);
void clearList(employeeRecord*[], int&);

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

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

getEmployeeData(empPtr, n);
calculateGrossPay(empPtr, n);
displayGrossPay(empPtr, n);
clearList(empPtr, n);

return 0;
}

//*********************************************************
// getEmployeeData
//
// Out Parameters: dataPtr, n
//
// Returns: void
//*********************************************************
void getEmployeeData(employeeRecord* dataPtr[], 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)
{
dataPtr[n] = new (nothrow) employeeRecord;
dataPtr[n]->identity = identity;
dataPtr[n]->hours = hours;
dataPtr[n]->wageRate = wageRate;
n = n+1;

infile >> identity >> wageRate >> hours;
}
infile.close();
}

//*********************************************************
// calculateGrossPay
//
// In Parameter: n
//
// In/Out Parameter: dataPtr
//
// Returns: void
//*********************************************************
void calculateGrossPay(employeeRecord* dataPtr[], int n)
{
for(int i = 0; i < n; i++)
{
if (dataPtr[i]->hours <= MAXHOURS)
{
dataPtr[i]->grossPay = dataPtr[i]->hours * dataPtr[i]->wageRate;
}
else
{
dataPtr[i]->grossPay = MAXHOURS * dataPtr[i]->wageRate
+ (dataPtr[i]->hours - MAXHOURS)
* OT_MULTIPLIER * dataPtr[i]->wageRate;
}
}
}

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

//*********************************************************
// clearList
//
// In/Out Parameters: dataPtr, n
//
// Return the memory allocated for the employee records
// back to the operating system and set n to 0.
//*********************************************************
void clearList(employeeRecord* dataPtr[], int& n)
{
for (int i = 0; i < n; i++)
{
delete dataPtr[i];
}
n = 0;
}
```
```//*********************************************************
// GrossPay11.cpp
//
// Calculate and display the gross pay given the hourly
// wage and the number of hours worked.
//
// This version uses an array of pointers to employee
// objects.
//
// D. Searls
// Asbury College
// Dec 2009
//*********************************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

const int MAXSIZE = 100;

class employeeRecord
{
private:

// Data members

string id;        // Employee identifier
double hours;     // Hours worked
double wage;      // Hourly wage

public:

// Class constructors

//-----------------------------------------------------
// Default Constructor
//
// Construct a new employee whose identifier is the
// empty string and whose hours and hourly wage are
// both zero.
//-----------------------------------------------------
employeeRecord()
{
id = "";
hours = 0.0;
wage = 0.0;
}

//-----------------------------------------------------
// Initialization Constructor
//
// Preconditions: empHours >= 0
//                empWage  >= 0
//
// Construct a new employee with the specified
// identifier, hours, and hourly wage. If the specified
// hours is negative, the employee's hours will be set
// to zero. If the specified hourly wage is negative,
// the employee's hourly wage will be set to zero.
//
// In Parameters: empId, empHours, empWage
//-----------------------------------------------------
employeeRecord(string empId, double empHours, double empWage)
{
id = empId;

if (empHours >= 0.0)
{
hours = empHours;
}
else
{
hours = 0.0;
}

if (empWage >= 0.0)
{
wage = empWage;
}
else
{
wage = 0.0;
}
}

//-----------------------------------------------------
// Copy Constructor
//
// Construct a new employee having the same state as
// the specified employee.
//
// In Parameter: emp
//-----------------------------------------------------
employeeRecord(const employeeRecord& emp)
{
id = emp.id;
hours = emp.hours;
wage = emp.wage;
}

// Member functions

//-----------------------------------------------------
// setIdentifier
//
// Set the employee's identifier to the specifed value.
//-----------------------------------------------------
void setIdentifier(string newId)
{
id = newId;
}

//-----------------------------------------------------
// setHours
//
// Precondition: newHours >= 0
//
// Set the employee's hours worked to the specifed
// value. If the specified value is negative, the
// employee's hours will not be changed.
//-----------------------------------------------------
void setHours(double newHours)
{
if (newHours >= 0.0)
{
hours = newHours;
}
}

//-----------------------------------------------------
// setWage
//
// Precondition: newWage >= 0
//
// Set the employee's hourly wage to the specifed value.
// If the specified value is negative, the employee's
// wage will not be changed.
//-----------------------------------------------------
void setWage(double newWage)
{
if (newWage >= 0)
{
wage = newWage;
}
}

//-----------------------------------------------------
// getIdentifier
//
// Get the employee's identifier.
//-----------------------------------------------------
string getIdentifier() const
{
return id;
}

//-----------------------------------------------------
// getHours
//
// Get the employee's hours worked.
//-----------------------------------------------------
double getHours() const
{
return hours;
}

//-----------------------------------------------------
// getWage
//
// Get the employee's hourly wage.
//-----------------------------------------------------
double getWage() const
{
return wage;
}

//-----------------------------------------------------
// getGrossPay
//
// Get the employee's gross pay.
//-----------------------------------------------------
double getGrossPay() const
{
const double MAXHOURS = 40.0;
const double OT_MULTIPLIER = 1.5;
double grossPay;

if (hours <= MAXHOURS)
{
grossPay = hours * wage;
}
else
{
grossPay = MAXHOURS * wage
+ (hours - MAXHOURS)
* OT_MULTIPLIER * wage;
}
return grossPay;
}
};

void getEmployeeData(employeeRecord*[], int&);
void calculateGrossPay(employeeRecord*[], int);
void displayGrossPay(employeeRecord*[], int);
void clearList(employeeRecord*[], int&);

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

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

getEmployeeData(empPtr, n);
displayGrossPay(empPtr, n);
clearList(empPtr, n);

return 0;
}

//*********************************************************
// getEmployeeData
//
// Out Parameters: dataPtr, n
//
// Returns: void
//*********************************************************
void getEmployeeData(employeeRecord* dataPtr[], 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)
{
dataPtr[n] = new (nothrow) employeeRecord;
dataPtr[n]->setIdentifier(identity);
dataPtr[n]->setHours(hours);
dataPtr[n]->setWage(wageRate);
n = n+1;

infile >> identity >> wageRate >> hours;
}
infile.close();
}

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

//*********************************************************
// clearList
//
// In/Out Parameters: dataPtr, n
//
// Return the memory allocated for the employee records
// back to the operating system and set n to 0.
//*********************************************************
void clearList(employeeRecord* dataPtr[], int& n)
{
for (int i = 0; i < n; i++)
{
delete dataPtr[i];
}
n = 0;
}```