C++ Inheritance. Any help with this would be greatly appreciated :) Write a clas
ID: 3684051 • Letter: C
Question
C++ Inheritance. Any help with this would be greatly appreciated :)
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)
// 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 // write clause here to derive from Employee base class.
{
private:
// declare The worker's shift as int
int shift;
// declare The worker's hourly pay rate as a double
double payRate;
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
{
// write the logic for the getshiftName() accessor
}
double getPayRate() const
{ return payRate; }
};
#endif
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.
Explanation / Answer
//This program demostrates a class and a derived class with constructors.
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
char EmpName;
int EmpNum;
int HireDate;
public:
void setEmpName(char);
void setEmpNum(int);
void setHireDate(int);
char getEmpName() const;
int getEmpNum() const;
int getHireDate() const;
Employee();
};
void Employee::setEmpName(char x)
{
EmpName = x;
}
void Employee::setEmpNum(int y)
{
EmpNum = y;
}
void Employee::setHireDate(int z)
{
HireDate = z;
}
char Employee::getEmpName() const
{
return EmpName;
}
int Employee::getEmpNum() const
{
return EmpNum;
}
int Employee::getHireDate() const
{
return HireDate;
}
Employee::Employee()
{
cout << "Give some details about the Employee. ";
}
class ProductionWorker : public Employee
{
private:
int Shift;
double HourlyPayRate;
public:
void setShift(int);
void setHourlyPayRate(double);
int getShift() const;
double getHourlyPayRate() const;
ProductionWorker();
};
void ProductionWorker::setShift(int a)
{
Shift = a;
}
void ProductionWorker::setHourlyPayRate(double b)
{
HourlyPayRate = b;
}
int ProductionWorker::getShift() const
{
return Shift;
}
double ProductionWorker::getHourlyPayRate() const
{
return HourlyPayRate;
}
ProductionWorker::ProductionWorker()
{
cout << "After this Employee's information will be displayed. ";
}
void main()
{
ProductionWorker info;
char name[100];
int num;
int date;
int shift;
double rate;
cout << "Employee's name: ";
cin.getline(name, 100);
cout << " Employee's ID number: ";
cin >> num;
cout << " Employee's hiring date:";
cout << "(Month, day, and year without any slashes, ";
cout << "dashes, commas, or other punctuation.) ";
cout << "For example, January 14, 1983 would look like 01141983. :";
cin >> date;
cout << "Employee work shift-1 or shift-2: ";
cin >> shift;
cout << "Employee per hour rate: ";
cin >> rate;
info.setEmpName(name[100]);
info.setEmpNum(num);
info.setHireDate(date);
info.setShift(shift);
info.setHourlyPayRate(rate);
cout << " Here is the employee's data: ";
cout << "Employee's Name: " << info.getEmpName() << endl;
cout << "Employee's Number: " << info.getEmpNum() << endl;
cout << "Employee's Hire Date: " << info.getHireDate() << endl;
cout << "Employee's Shift: " << info.getShift() << endl;
cout << setprecision(2) << fixed;
cout << "Employee's Hourly Pay Rate: $" << info.getHourlyPayRate() << endl << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.