In: Computer Science
11. Payroll in C++
Design a PayRoll class that has data members for an employee’s
hourly pay rate, number of hours worked of type double. The default
constructor will set the hours worked and pay rate to zero. The
class must have a mutator function to set the pay rate for each
employee and hours worked. The class should include accessors for
both the hours worked and the rate of pay. The class should lastly
have a getGross function that will return a double calculated by
multiplying the hours worked by the rate
of pay.
Write a program with an array of seven PayRoll objects. The program
should ask the user for the rate of pay for each employee and the
number of hours each employee has worked. Be sure to include an
employee claiming to work more then 60 hours per week. Print out,
the array number of the employee, the hours worked, the rate of
pay, and the gross pay, of all the employee's each on their own
line. Set the precision for printing the doubles to two decimal
places.
Input Validation: Do not accept values greater than 60 for the
number of hours worked, simply have the set function set number of
hours worked to 60, the maximum allowed.
Data output:
Creating◦7◦Payroll◦objects.⏎
Enter◦the◦#◦0◦employee's◦rate◦of◦pay◦per◦hour:Enter◦the◦#◦0◦employee's◦hours◦worked◦for◦the◦week:Enter◦the◦#◦1◦employee's◦rate◦of◦pay◦per◦hour:Enter◦the◦#◦1◦employee's◦hours◦worked◦for◦the◦week:Enter◦the◦#◦2◦employee's◦rate◦of◦pay◦per◦hour:Enter◦the◦#◦2◦employee's◦hours◦worked◦for◦the◦week:Enter◦the◦#◦3◦employee's◦rate◦of◦pay◦per◦hour:Enter◦the◦#◦3◦employee's◦hours◦worked◦for◦the◦week:Enter◦the◦#◦4◦employee's◦rate◦of◦pay◦per◦hour:Enter◦the◦#◦4◦employee's◦hours◦worked◦for◦the◦week:Enter◦the◦#◦5◦employee's◦rate◦of◦pay◦per◦hour:Enter◦the◦#◦5◦employee's◦hours◦worked◦for◦the◦week:Enter◦the◦#◦6◦employee's◦rate◦of◦pay◦per◦hour:Enter◦the◦#◦6◦employee's◦hours◦worked◦for◦the◦week:⏎
⏎
Employee◦#◦0◦worked◦22.00◦hours◦at◦$12.22◦for◦a◦gross◦pay◦of◦$268.84⏎
Employee◦#◦1◦worked◦36.00◦hours◦at◦$99.99◦for◦a◦gross◦pay◦of◦$3599.64⏎
Employee◦#◦2◦worked◦20.00◦hours◦at◦$45.67◦for◦a◦gross◦pay◦of◦$913.40⏎
Employee◦#◦3◦worked◦0.00◦hours◦at◦$56.78◦for◦a◦gross◦pay◦of◦$0.00⏎
Employee◦#◦4◦worked◦5.00◦hours◦at◦$40.45◦for◦a◦gross◦pay◦of◦$202.25⏎
Employee◦#◦5◦worked◦60.00◦hours◦at◦$30.35◦for◦a◦gross◦pay◦of◦$1821.00⏎
Employee◦#◦6◦worked◦45.00◦hours◦at◦$30.33◦for◦a◦gross◦pay◦of◦$1364.85⏎
CODE
#include<iostream>
#include<iomanip>
using namespace std;
//class PayRoll
class PayRoll
{
//private section
private:
//data members
double pay_rate, worked_hours;
//public section
public:
//default constructor
PayRoll()
{
//initializing data members
pay_rate = 0;
worked_hours = 0;
}
//mutator function to set pay rate
void set_pay_rate(double rate)
{
//setting pay rate
pay_rate = rate;
}
//mutator function to set hours worked
void set_worked_hours(double hours)
{
//if hours worked greater than 60
if(hours > 60)
{
//sets worked hours as 60
worked_hours = 60;
}
//otherwise
else
{
//setting worked hours
worked_hours = hours;
}
}
//accessor function for get pay rate
double get_pay_rate()
{
//returns pay rate
return pay_rate;
}
//accessor function for get worked hours
double get_worked_hours()
{
//returns worked hours
return worked_hours;
}
//getGross function to return gross pay
double getGross()
{
//returns gross pay
return worked_hours * pay_rate;
}
};
//main function
int main()
{
//variables
double rate, hours;
//creating 7 payroll objects
PayRoll ob[7];
cout << "Creating 7 Payroll objects." << endl;
//loop to read data
for(int i = 0; i < 7; i++)
{
//prompt for pay rate for each employee
cout << "Enter the #" << i << " employee's rate of pay per hour: ";
cin >> rate;
//prompt for worked hours for each employee
cout << "Enter the #" << i << " employee's hours worked for the week: ";
cin >> hours;
//calling set_pay_rate
ob[i].set_pay_rate(rate);
//calling set_worked_hours
ob[i].set_worked_hours(hours);
}
cout << endl;
//loop to print output
for(int i = 0; i < 7; i++)
{
//printing each employee's worked hours, pay rate and gross pay
cout << fixed << setprecision(2) << "Employee #" << i << " worked " << ob[i].get_worked_hours() << " hours at $" << ob[i].get_pay_rate() << " for a gross pay of $" << ob[i].getGross() << endl;
}
return 0;
}
OUTPUT
CODE SCREEN SHOT