Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Inheritance Define an Employee class that has the following protected members: a

ID: 3730295 • Letter: I

Question

Inheritance Define an Employee class that has the following protected members: a string to store the employee's name and an integer to store the number of years the employee has worked. Add appropriate constructors, accessor functions, and mutator functions. Also define a function named printBonus ( that prints the end of the year bonus for an employee, which is "Unknown" for an Employee object. void printBonus Next, define an Administrator class that is derived from Employee. The Administrator class should have a private member variable named title that stores the Administrator's title as a string. Add appropriate constructors, mutator functions, and accessor functions for the sponsor variable. Redefine the printBonus ( function to print "Your bonus is $500!" if the Administrator has worked 10 or more years, or "Your bonus is $200!" if the Administrator has worked fewer than 10 years. Finally, create an application that does the following: Create an Employee object and an Administrator pointer. Ask the user for the information to set the data members for the Employee object. Print the end of the year bonus for the Employee. Ask the user for the number of administrators. Create a dynamically-allocated array of Administrator objects. Ask the user for the information to set the data members for each Administrator object. Print the end of the year bonus for all the administrators. Don't forget to delete your Administrators! .

Explanation / Answer

#include <iostream>

using namespace std;

class Employee{
private:
string name;
int numberOfYears;

public:
Employee(){
}
Employee(string n, int y){
name = n;
numberOfYears = y;
}
string getName() {
return name;
}
void setName(string n) {
name = n;
}
int getNumberOfYears() {
return numberOfYears;
}
void setNumberOfYears(int y) {
numberOfYears = y;
}
void printBonus() {
cout << "Your bonus is Unknown! ";
}
};

class Administrator : public Employee{
private:
string title;

public:
Administrator(string n, int y, string t) : Employee(n, y) {
title = t;
}
Administrator(){
}
string getTitle() {
return title;
}
void setTitle(string t) {
title = t;
}
void printBonus() {
if(getNumberOfYears() >= 10) {
cout << "Your bonus is $500! ";
}
else {
cout << "Your bonus is $200! ";
}
}

};

int main(){
Employee emp;
Administrator *admin;
string name;
int numOfYears;
cout << "Enter employee's name: ";
cin >> name;
cout << "Enter employee's number of years: ";
cin >> numOfYears;
emp.setName(name);
emp.setNumberOfYears(numOfYears);
emp.printBonus();
int numOfAdmins;
cout << "Enter the number of administrators: ";
cin >> numOfAdmins;
admin = new Administrator[numOfAdmins];

string title;
for(int i = 0; i < numOfAdmins; i++) {
cout << "Enter name of admin " << i + 1 << ": ";
cin >> name;
cout << "Enter num of years: ";
cin >> numOfYears;
cout << "Enter title: ";
cin >> title;
admin[i].setName(name);
admin[i].setNumberOfYears(numOfYears);
admin[i].setTitle(title);
admin[i].printBonus();
}
}