Design a class named Employee. The class should keep the following information i
ID: 3773963 • Letter: D
Question
Design a class named Employee. The class should keep the following information in fields: Employee name Employee number in the format XXX-L, where each X is a digit within the range 0-9 and the L is a letter within the range A-M. Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields 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 field will be 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 methods for the class. Demonstrate the classes by writing a program that uses a ProductionWorker object.Explanation / Answer
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
//Class Employee
class Employee
{
//Private member
string empName, empCode, hireDate;
public:
//Constructor
Employee();
Employee(string, string, string);
//Accessor and Mutator methods
void setEmpName(string);
void setEmpCode(string);
void setHireDate(string);
string getEmpName();
string getEmpCode();
string getHireDate();
};
//Sets the employee name
void Employee::setEmpName(string s)
{
empName = s;
}
//Sets the employee code
void Employee::setEmpCode(string c)
{
empCode = c;
}
//Sets the employee hire date
void Employee::setHireDate(string d)
{
hireDate = d;
}
//Returns employee name
string Employee::getEmpName()
{
return empName;
}
//Returns employee code
string Employee::getEmpCode()
{
return empCode;
}
//Returns employee hire date
string Employee::getHireDate()
{
return hireDate;
}
//Parameterized constructor for employee
Employee::Employee(string n, string c, string d)
{
empName = n;
empCode = c;
hireDate = d;
}
//Default constructor for employee
Employee::Employee()
{
empName = "";
empCode = "";
hireDate = "";
}
//class production worker derived from employee
class ProductionWorker : public Employee
{
//Private data member
int Shift;
double HourlyPayRate;
public:
//Default constructor
ProductionWorker()
{
Shift = 0;
HourlyPayRate = 0.0;
}
//Parameterized constructor calls base class constructor
ProductionWorker(int a, double r, string n, string c, string d): Employee(n, c, d)
{
Shift = a;
HourlyPayRate = r;
}
//Set shift
void setShift(int s)
{
Shift = s;
}
//Set the hourly pay rate
void setHourlyPayRate(double r)
{
HourlyPayRate = r;
}
//Returns shift
int getShift()
{
return Shift;
}
//Returns hourly pay rate
double getHourlyPayRate()
{
return HourlyPayRate;
}
};
int main()
{
string na, co, da, vc;
int t = 0, sh, vsh;
double pr;
cout<<" Enter Employee Name: ";
cin>>na;
//Validates employee code
do
{
cout<<" Enter Employee Code: XXXL Each X is a digit within the range 0 - 9 L is a letter within the range A - M ";
fflush(stdin);
cin>>co;
//Checks for the length
if(co.length() == 4)
{
//Checks the first 3 letters must be digit
if(isdigit(co[t]) && isdigit(co[t+1]) && isdigit(co[t+2]))
{
//checks the first 3 letters must be in the range of 0 - 9
if((co[t] >= '0' && co[t] <= '9') &&(co[t+1] >= '0' && co[t+1] <= '9') && (co[t+2] >= '0' && co[t+2] <= '9'))
{
//Checks 4 th letter must be a alphabet and within the range of A to M
if(isalpha(co[t+3]) && co[t+3] >= 'A' && co[t+3]<= 'M')
{
vc = co;
break;
}
else
cout<<" Error: Must be alphabet within the range of A - M ";
}
else
cout<<" Error: Must be within the range of 0 - 9 ";
}
cout<<" Error: Must be Digit ";
}
else
{
cout<<" Error: Must be 4 letters ";
}
}while(1);
cout<<" Enter the Hire Date: ";
cin>>da;
//Validates the shift
do
{
cout<<" Enter the shift: ";
cin>>sh;
//Shift must be either 1 or 2
if(sh == 1 || sh == 2)
{
vsh = sh;
break;
}
else
cout<<" Error: Shift must be either 1 or 2 ";
}while(1);
cout<<" Enter Hourly pay rate: ";
cin>>pr;
ProductionWorker p;
ProductionWorker p1(vsh, pr, na, vc, da);
cout<<" Employee Information: ";
cout<<" Employee Name: "<<p1.getEmpName();
cout<<" Employee Code: "<<p1.getEmpCode();
cout<<" Employee Hire Date: "<<p1.getHireDate();
cout<<" Employee Shift: "<<p1.getShift();
cout<<" Employee Hourly Pay Rate: "<<p1.getHourlyPayRate();
}
Output 1:
Enter Employee Name: Mohan
Enter Employee Code: XXXL
Each X is a digit within the range 0 - 9
L is a letter within the range A - M
234B
Enter the Hire Date: 23
Enter the shift: 1
Enter Hourly pay rate: 30
Employee Information:
Employee Name: Mohan
Employee Code: 234B
Employee Hire Date: 23
Employee Shift: 1
Employee Hourly Pay Rate: 30
Output 2:
Enter Employee Name: Mohan
Enter Employee Code: XXXL
Each X is a digit within the range 0 - 9
L is a letter within the range A - M
12AB
Error: Must be Digit
Enter Employee Code: XXXL
Each X is a digit within the range 0 - 9
L is a letter within the range A - M
456T
Error: Must be alphabet within the range of A - M
Error: Must be Digit
Enter Employee Code: XXXL
Each X is a digit within the range 0 - 9
L is a letter within the range A - M
782D
Enter the Hire Date: 11
Enter the shift: 5
Error: Shift must be either 1 or 2
Enter the shift: 2
Enter Hourly pay rate: 50
Employee Information:
Employee Name: Mohan
Employee Code: 782D
Employee Hire Date: 11
Employee Shift: 2
Employee Hourly Pay Rate: 50
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.