Employee and Production Worker Classes C++
Employee and ProductionWorker Classes
Design a C++ class named Employee. The class should keep the following information in
member variables:
• Employee name
• Employee number
• Hire date
Write one or more constructors and the appropriate accessor and mutator functions
for the class.
Next, write a class named ProductionWorker that is derived from the Employee
class. The ProductionWorker class should have member variables to hold the following
information:
• Shift (an integer)
• Hourly pay rate (a double)
The workday is divided into two shifts: day and night. The shift variable will hold an
integer value representing the shift that the employee works. The day shift is shift 1
and the night shift is shift 2. Write one or more constructors and the appropriate
accessor and mutator functions for the class. Demonstrate the classes by writing a
program that uses a ProductionWorker object.
Answer:
Employee and ProductionWorker classes
#include <iostream> #include <iomanip> #include "ProductionWorker.h" using namespace std; // Function prototype void displayInfo(ProductionWorker); int main() { ProductionWorker pw("John Jones", "123", "1/1/2006", 2, 18.00); displayInfo(pw); return 0; } //****************************************************** // The displayInfo function displays a production * // worker's employment information. * //****************************************************** void displayInfo(ProductionWorker e) { cout << setprecision(2) << fixed << showpoint; cout << "Name: " << e.getName() << endl; cout << "Employee number: " << e.getNumber() << endl; cout << "Hire date: " << e.getHireDate() << endl; cout << "Shift: " << e.getShiftName() << endl; cout << "Shift number: " << e.getShiftNumber() << endl; cout << "Pay rate: " << e.getPayRate() << endl; }
Specification file for the ProductionWorker Class
#ifndef PRODUCTION_WORKER_H #define PRODUCTION_WORKER_H #include "Employee.h" #include <string> using namespace std; class ProductionWorker : public Employee { private: int shift; // The worker's shift double payRate; // The worker's hourly pay rate public: // Default constructor ProductionWorker() : Employee() { shift = 0; payRate = 0.0; } // Constructor ProductionWorker(string aName, string aNumber, string aDate, int aShift, double aPayRate) : Employee(aName, aNumber, aDate) { shift = aShift; payRate = aPayRate; } // Mutators void setShift(int s) { shift = s; } void setPayRate(double r) { payRate = r; } // Accessors int getShiftNumber() const { return shift; } string getShiftName() const { if (shift == 1) return "Day"; else if (shift == 2) return "Night"; else return "Invalid"; } double getPayRate() const { return payRate; } }; #endif
Specification file for the Employee class
#ifndef EMPLOYEE_H #define EMPLOYEE_H #include <string> using namespace std; class Employee { private: string name; // Employee name string number; // Employee number string hireDate; // Hire date public: // Default constructor Employee() { name = ""; number = ""; hireDate = ""; } // Constructor Employee(string aName, string aNumber, string aDate) { name = aName; number = aNumber; hireDate = aDate; } // Mutators void setName(string n) { name = n; } void setNumber(string num) { number = num; } void setHireDate(string date) { hireDate = date; } // Accessors string getName() const { return name; } string getNumber() const { return number; } string getHireDate() const { return hireDate; } }; #endif
Leave a reply