The purpose of this assignment is to demonstrate how to work with pointers as function parameters, and with the close relationship between pointers and arrays. Along with these assignment instructions you should find a UML diagram outlining the class members with details about the constructor and other member function definitions.

For this assignment, you will need to define a simple class that encapsulates a pay roll object including hours worked, pay rate, and related functions including one to find the gross pay based on hours worked and pay rate. The main program will define an array of these objects and then simply calls two functions to first input data and store it in each object in the array, and then output the gross pay for each object. In the process, you will demonstrate how to pass the array as an argument to functions using a pointer parameter to work with the pay roll objects. The input function can use a FOR loop to read in the data either by prompting and reading from the keyboard or simply read directly from an input file. The output function also can use a FOR loop to write the gross pay to the screen for each one.

Objectives to be met

  • Demonstrate how declare functions with pointer parameters.
  • Demonstrate how to call functions with arguments passed to pointer parameters.
  • Demonstrate how dereference pointers inside functions with pointer parameters.
  • Follow the PayRoll UML diagram specifications and implementation details. Use separate files for PayRoll.h and PayRoll.cpp
  • In the programs main() function, define an array of 7 PayRoll objects.
  • Declare, call, and define an input function that will loop to read the hours and pay rate for 7 employees, storing each set of data in each of the PayRoll objects. This is a void function with 2 parameters: a PayRoll pointer, and an int for the array size.
  • Declare, call, and define an output function that will print the gross pay for 7 employees. This is another void function with 2 parameters: a PayRoll pointer, and an int for the array size.
  • Revise the declarations and headings in both functions to use const parameters. With the input function, the pointer and size should be constants but the PayRoll data cannot be a constant. With the output function, both the pointer and PayRoll data, and the size all should be constant parameters.
  • Revise the array definition inside main() to dynamically allocate the array, and at the end of the program just before the return statement, release the dynamically allocated memory; you will need to store that address in a pointer variable and then decide whether or not anything else must be modified.
  • Revise the private data members in the PayRoll class to be pointers. In the constructor you should initialize the pointers with dynamically allocated memory. Add a destructor that will release the dynamically allocated memory. Finally, modify all the other member functions as necessary to dereference the pointer data members.

Considerations:

  • The input function will need two double variables to read the input and then use the next PayRoll object (array element) to call the member functions that store the data in that object.
  • The output function will need to call the member function for each object that calculates and returns the gross pay as it loops through the array. The employee numbers shown in the output example can be printed by adding 1 to the loop control variable as it iterates through the array.
  • Because of the close relationship between pointers and arrays, both functions can use array [] notation to select each object in the array and the dot operator to call the appropriate member function. Or, you can use pointer arithmetic and the -> operator to dereference an address and select the function. (Consider demonstrating both when you pass a pointer to a function there are 4 different ways to indicate how a constant might be applied: Parameter declaration constant data constant pointer
DataType* ptr no no
DataType* const ptr no yes
const DataType* ptr yes no
const DataType* const ptr yes yes
  • Choose the appropriate form of declaration based on the instructions for that recommendation.
  • remember to use the correct syntax when using delete at the end of the program to release all the dynamic memory allocated for the array.

Given this set of input data

Hours Pay Rate
40.0 10.00
38.5 9.50
16.0 7.50
22.5 9.50
43.0 8.00
38.0 8.00
45.0 9.00

Sample program output

Employee Gross pay
======== =========
1: $ 400.00
2: $ 365.75
3: $ 120.00
4: $ 213.75
5: $ 356.00
6: $ 304.00
7: $ 427.50
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.