Question

In: Computer Science

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 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 . . .

Solutions

Expert Solution



#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.


Related Solutions

Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement each member function in the class below. Some of the functions we may have already done in the lecture, that's fine, try to do those first without looking at your notes. You may add whatever private data members or private member functions you want to this class. #include #include typedef std::string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private:...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined...
Program Task (C++) The program should contain an abstract data type (ADT) for an Employee, defined as follows: struct Employee int id; // unique employee identifier string name; // employee’s full name double rate; // employee’s hourly rate double hours; // how many hours they worked since last pay double taxable; // the current year’s taxable income }; This definition should appear above the main() function. The main() function should include: 1. Declare a single array to hold at most...
C. Design a RandomCipher class as a subclass of the substitutionCipher from exercise P-3.40, so that...
C. Design a RandomCipher class as a subclass of the substitutionCipher from exercise P-3.40, so that each instance of the class relies on a random permutation of letters for its mapping.
Payroll Register The following data for Throwback Industries, Inc. relate to the payroll for the week...
Payroll Register The following data for Throwback Industries, Inc. relate to the payroll for the week ended December 9, 20Y8: Hours Hourly Weekly Federal Retirements Employee Worked Rate Salary Income Tax Savings Aaron 46 $30 $338.10 $75 Cobb 41 42 374.75 40 Clemente 43 26 219.83 100 DiMaggio 36 40 302.4 90 Griffey, Jr. 48 38 414.96 50 Mantle $2,280 547.20 65 Robinson 35 34 178.50 40 Williams 2,550 561.00 65 Vaughn 44 46 423.20 40 Employees Mantle and Williams...
Payroll Register The following data for Throwback Industries Inc. relate to the payroll for the week...
Payroll Register The following data for Throwback Industries Inc. relate to the payroll for the week ended December 9, 20Y8: Hours Hourly Weekly Federal Retirements Employee Worked Rate Salary Income Tax Savings Aaron 45 $46 $502.55 $105 Cobb 42 32 295.84 45 Clemente 41 24 189.24 90 DiMaggio 37 34 264.18 85 Griffey, Jr. 44 42 405.72 40 Mantle $2,280 547.20 75 Robinson 35 36 189.00 35 Williams 2,550 561.00 80 Vaughn 48 28 291.20 35 Employees Mantle and Williams...
Payroll Register The following data for Throwback Industries Inc. relate to the payroll for the week...
Payroll Register The following data for Throwback Industries Inc. relate to the payroll for the week ended December 9, 20Y8: Hours Hourly Weekly Federal Retirements Employee Worked Rate Salary Income Tax Savings Aaron 43 $26 $266.11 $40 Cobb 48 44 491.92 40 Clemente 45 34 306.85 50 DiMaggio 35 28 205.8 55 Griffey, Jr. 41 42 366.03 90 Mantle $2,040 489.60 70 Robinson 36 30 162.00 110 Williams 2,280 501.60 70 Vaughn 44 40 368.00 95 Employees Mantle and Williams...
Payroll Register The following data for Throwback Industries Inc. relate to the payroll for the week...
Payroll Register The following data for Throwback Industries Inc. relate to the payroll for the week ended December 9, 20Y8: Hours Hourly Weekly Federal Retirements Employee Worked Rate Salary Income Tax Savings Aaron 43 $26 $266.11 $105 Cobb 48 44 491.92 100 Clemente 45 34 306.85 90 DiMaggio 37 28 217.56 80 Griffey, Jr. 41 42 366.03 70 Mantle $2,290 549.60 65 Robinson 35 30 157.50 85 Williams 2,560 563.20 90 Vaughn 44 40 368.00 90 Employees Mantle and Williams...
Payroll Register The following data for Throwback Industries, Inc. relate to the payroll for the week...
Payroll Register The following data for Throwback Industries, Inc. relate to the payroll for the week ended December 9: Hours Hourly Weekly Federal U.S. Savings Employee Worked Rate Salary Income Tax Bonds Blanda 42 $30 $296.70 $90 Dawson 46 42 442.47 105 Fouts 43 26 219.83 105 Griese 34 40 285.6 80 Namath 48 38 414.96 45 Marino $2,030 487.20 85 Staubach 38 34 193.80 95 Starr 2,270 499.40 95 Unitas 45 46 437.00 80 Employees Marino and Starr are...
C++ exercise ! Change the WEEK‐4 program to work through the GradeBook class. This program has...
C++ exercise ! Change the WEEK‐4 program to work through the GradeBook class. This program has some functionality that you can use with the Gradebook class. So, please revise Gradebook class so that the class can use sort() and display() functions of WEEK4 program . week-4 program : #include #include using namespace std; void sort(char nm[][10]); void display(char name[][10]); int main() { char names[10][10]; int i; for (i=0; i<10; i++) { cout << "Enter name of the student : ";...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT