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

Implement a program as an object using a class (abstract data type) in C++ that does...
Implement a program as an object using a class (abstract data type) in C++ that does the following: 1) reads the firstName, lastName and 3 test scores of at least five students. 2) calculate student test score totals, average, letter grade for each student. 3) Display the results in a table format showing firstName, lastName, test1, test2, test3, total, average, letterGrade, of all the students. 3 files .h, .cpp, main.cpp create an object that can hold records. must get records...
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:...
Write a c++ class definition for an abstract data type describing a bookstore inventory. Each book...
Write a c++ class definition for an abstract data type describing a bookstore inventory. Each book has the following attributes: Book Title (character string); Book Author (character string); Book Price (Floating point number having two decimal places); Count of books on hand (int); The member functions are as follows: A constructor that is used to initialize all four elements of the structure to values inputted by the user; A function that displays in a readable tabular form the contents of...
Programming Language Concept assignment: 1. Design abstract data type for matrices with integer elements in C++...
Programming Language Concept assignment: 1. Design abstract data type for matrices with integer elements in C++ language, including operations for matrix addition, subtraction, and multiplication! 2. Design abstract queue data types for float elements in C++ language, including operations for enqueue, dequeue, and empty. The dequeue operation removes the element and returns its value! 3. Set semaphores in Ada and use them to provide co-operation and synchronization of competitions in shared buffer instances!
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...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT