In: Computer Science
You will take the example from the Code Along where we wrote the Employee and ProductionWorker class and modify.
In a particular factory, a team leader is an hourly paid production worker who leads a small team. In addition to hourly pay, team leaders earn a fixed monthly bonus, Team leaders are required to attend a minimum number of hours of training per year. Design a TeamLeader class that extends the ProductionWorker class we designed together. The TeamLeader class should have member variables for the monthly bonus amount, the required number of training hours, and the number of training hours that the team leader has attended. Write one or more constructors and the appropriate accessor and mutator functions for the class. Demonstrate the class by writing a program that uses a TeamLeader object. Please type whole Program. Make sure you type which is main which is class.h. This is CPP
Here is Employee.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
string name;
string number;
string hireDate;
public:
Employee()
{
name = ""; number = ""; hireDate =
"";
}
Employee(string aName, string aNumber, string
aDate)
{
name = aName; number = aNumber;
hireDate = aDate;
}
void setName(string n)
{
name = n;
}
void setNumber(string num)
{
number = num;
}
void setHireDate(string date)
{
hireDate = date;
}
string getName() const
{
return name;
}
};
Here is ProducitonWorker.h
#pragma once
#include"Employee.h"
#include <string>
using namespace std;
class ProductionWorker : public Employee
{
private:
int shift;
double payRate;
public:
ProductionWorker() :Employee()
{
shift = 0; payRate = 0.0;
}
ProductionWorker(string aName, string aNumber, string
aDate, int aShift, double aPayRate) :Employee(aName, aNumber,
aDate)
{
shift = aShift; payRate =
aPayRate;
}
void setShift(int s)
{
shift = s;
}
void setPayRate(double r)
{
payRate = r;
}
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;
}
};
In case of any query do comment. Please rate answer as well. Thanks
Code:
=====main.cpp=======
#include "TeamLeader.h"
int main()
{
//Create an object of TeamLeader
TeamLeader tl("ALex", "123", "23NOV2011", 2, 40,1000, 35,40);
//display object properties
cout << "Name: " << tl.getName() << endl;
cout << "Shift: " << tl.getShiftNumber() << endl;
cout << "Payrate: " << tl.getPayRate()<< endl;
cout << "MonthlyBonus: " << tl.getMonthlyBonus()<< endl;
cout << "RequiredTrainingHours: " << tl.getRequiredTrainingHours()<< endl;
cout << "AttendedTrainingHours: " << tl.getAttendedTrainingHours()<< endl;
cout << "Name: " << tl.getName()<< endl;
return 0;
}
=============TeamLeader.h=========
#pragma once
#include"ProducitonWorker.h"
#include <string>
using namespace std;
class TeamLeader: public ProductionWorker
{
private:
double monthlyBonus;
int requiredTrainingHours;
int attendedTrainingHours;
public:
//constructors
TeamLeader(): ProductionWorker()
{
monthlyBonus=0.0;
requiredTrainingHours=0;
attendedTrainingHours=0;
}
TeamLeader(string aName, string aNumber, string aDate, int aShift, double aPayRate, double aMonthlyBonus, int aRequiredTrainingHours, int aAttendedTrainingHours):
ProductionWorker(aName, aNumber, aDate, aShift, aPayRate)
{
monthlyBonus = aMonthlyBonus; requiredTrainingHours = aRequiredTrainingHours;; attendedTrainingHours = aAttendedTrainingHours;
}
//mutators
void setMonthlyBonus(double b)
{
monthlyBonus = b;
}
void setRequiredTrainingHours(int rth)
{
requiredTrainingHours = rth;
}
void setAttendedTrainingHours(int ath)
{
attendedTrainingHours = ath;
}
//accessors
double getMonthlyBonus() const
{
return monthlyBonus;
}
int getRequiredTrainingHours() const
{
return requiredTrainingHours;
}
int getAttendedTrainingHours() const
{
return attendedTrainingHours ;
}
};
===========Screen shot of the code===========
output: