In: Computer Science
Week 3 In-Class Exercise C++
Payroll
Design a PayRoll class that is an abstract data type for payroll. It has data members for an employee’s hourly pay rate, number of hours worked, and total pay for the week.
Your class must include the following member functions:
a constructor to set the hours and pay rate as arguments,
a default constructor to set data members to 0,
member functions to set each of the member variables to values given as an argument(s) to the function,
member functions to retrieve the data from each of the member variables,
an input function that reads the values of number of hours and hourly pay rate,
an output function that outputs the value of all data members for an employee,
void function to calculate the total pay for a week.
The input and output functions will each have one formal parameter for the stream.
Write a program with an array of seven PayRoll objects. The program should ask the user for the number of hours each employee has worked and will then display the amount of gross pay each has earned.
Input Validation: Do not accept values greater than 60 for the number of hours
worked.
Sample Output:
Enter the hours worked and pay rate for 7 employees:
Employee #1 pay rate: 15
Employee #1 hours worked: 40
Employee #2 pay rate: 20
Employee #2 hours worked: 35
Employee #3 pay rate: 18
Employee #3 hours worked: 40
Employee #4 pay rate: 22
Employee #4 hours worked: 38
Employee #5 pay rate: 15
Employee #5 hours worked: 40
Employee #6 pay rate: 20
Employee #6 hours worked: 32
Employee #7 pay rate: 22
Employee #7 hours worked: 40
Total pay:
Employee #1: 600.00
Employee #2: 700.00
Employee #3: 720.00
Employee #4: 836.00
Employee #5: 600.00
Employee #6: 640.00
Employee #7: 880.00
Press any key to continue . . .
#include <iostream> using namespace std; class PayRoll { double payRate; int numHours; double totalPay; public: // a constructor to set the hours and pay rate as arguments, PayRoll(double rate, int hours) { payRate = rate; numHours = hours; calculatePay(); } // a default constructor to set data members to 0, PayRoll() { payRate = 0; numHours = 0; totalPay = 0; } // member functions to set each of the member variables to values given as an argument(s) to the function, void setPayRate(double rate) { payRate = rate; calculatePay(); } void setNumHours(double hours) { numHours = hours; calculatePay(); } // void function to calculate the total pay for a week. void calculatePay() { totalPay = payRate * numHours; } // member functions to retrieve the data from each of the member variables, double getTotalPay() { calculatePay(); return totalPay; } // an input function that reads the values of number of hours and hourly pay rate, void input() { cout << "Enter payrate: "; cin >> payRate; cout << "Enter number of hours worked: "; cin >> numHours; calculatePay(); } // an output function that outputs the value of all data members for an employee, void output() { cout << "Payrate: " << payRate << endl; cout << "hours Worked: " << numHours << endl; cout << "Total Pay: " << getTotalPay() << endl; } }; int main() { PayRoll data[7]; for(int i=1; i<=7; i++) { int numHours; double payRate; cout << "Employee #" << i << " pay rate: "; cin >> payRate; cout << "Employee #" << i << " hours worked: "; cin >> numHours; while(numHours > 60) { cout << "Error: hours should be less than 60." << endl; cout << "Enter number of hours worked: "; cin >> numHours; } data[i-1].setPayRate(payRate); data[i-1].setNumHours(numHours); } cout << endl << "total Pay:" << endl; for(int i=1; i<=7; i++) { cout << "Employee #" << i << ": " << data[i-1].getTotalPay() << endl; } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.