Question

In: Computer Science

11. Payroll in C++ Design a PayRoll class that has data members for an employee’s hourly...

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⏎

Solutions

Expert Solution

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


Related Solutions

Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type...
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...
c++ Design the Weather class that contains the following members: Data members to store: -a day...
c++ Design the Weather class that contains the following members: Data members to store: -a day (an integer) -a month (an integer) -a year (an integer) -a temperature (a float) -a static data member that stores the total of all temperatures (a float) Member functions: -a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly entered temperature to the total). -a static...
C++ Assignment. Design the Weather class that contains the following members: Data members to store: -...
C++ Assignment. Design the Weather class that contains the following members: Data members to store: - a day (an integer) - a month (an integer) - a year (an integer) - a temperature (a float) - a static data member that stores the total of all temperatures (a float) Member functions: - a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
C++ Define a MyDate class to represent a data that has 3 data members: day, month,...
C++ Define a MyDate class to represent a data that has 3 data members: day, month, year. The class should have: A default constructor to initialize the day, month, and year to 0. A constructor that accepts three integers as arguments to initialize the day, month, and year if the three arguments are valid. A copy constructor that accepts a reference to a MyDate object as argument. This constructor use the argument's three data fields to initialize the day, month,...
2. Report Heading Design a class called Heading that has data members to hold the company...
2. Report Heading Design a class called Heading that has data members to hold the company name and the report name. A two-parameter default constructor should allow these to be specified at the time a new Heading object is created. If the user creates a Heading object without passing any arguments, “ABC Industries” should be used as a default value for the company name and “Report” should be used as a default for the report name. The class should have...
Design a Ship class that has the following members: • A member variable for the name...
Design a Ship class that has the following members: • A member variable for the name of the ship (a string) • A member variable for the year that the ship was built (a string) • A constructor and appropriate accessors and mutators • A virtual print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: • A...
C++ Define a class PersonalRecord, which will be storing an employee’s information. Internally, the class should...
C++ Define a class PersonalRecord, which will be storing an employee’s information. Internally, the class should have the following attributes: SSN (string type), fullName (string type), homeAddress (string type), phoneNumber (string type), salaryRate (per year, float type), and vacationDays (accrued vacation, int type). The constructor should take six parameters: four of them are necessary to provide values to at the time of initialization (when a constructor will be called/invoked): SSN, fullName, homeAddress, and phoneNumber; and two of them are optional...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ CLASSES 1. Define a class date that has day, month and year data members. 2....
C++ CLASSES 1. Define a class date that has day, month and year data members. 2. Define a class reservation with following data members: - An integer counter that generates reservation numbers. - An integer as a reservation number. The counter is incremented each time a reservation object is created. This value will be assigned as the reservation number. - Number of beds. - Reservation date from class date. 3. Define a class rooms with data members: - Room number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT