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

INHERITANCE, POLYMORPHISM AND STL C++ PROGRAM HELP OBJECTIVE Practice using inhe

ID: 3828707 • Letter: I

Question

INHERITANCE, POLYMORPHISM AND STL C++ PROGRAM HELP

OBJECTIVE

Practice using inheritance

Practice using polymorphism

Practice using an STL container

For this project, you will write a complete C++ program to handle a staff directory and payroll. Extend the driver code located in project6-main.cpp. You need to declare an STL container named employees in the processInputs() function. Additionally, you need to write the Employee, Cashier and Manager classes. Also, you need to use inheritance and polymorphism appropriately. The Employee class needs to store the employee's name. Employees are paid minimum wage ($7.25) for 40 hours each week. The Cashier class needs to store the cashier name and their hourly wage. Cashiers are paid this hourly wage for 40 hours each week. The Manager class needs to store the manager's name, their ID and their weekly salary.
Additionally, add a comment identifying the polymorphic method call in project6-main.cpp.
You can use the following input files for this project:

DRIVER CODE // project6-main.cpp

OUTPUT

RUBRIC:

Explanation / Answer

Given below is implementation of three C++ classes, updated driver program along with sample test data and execution output.


File: Employee.h

#ifndef Included_Employee_H
#define Included_Employee_H

#include <iostream>
using namespace std;

class Employee
{
private:
static const float NUM_WORK_HOURS = 40.0;
static const float MIN_WAGE_PER_HOUR = 7.25; // $7.25

protected:
string name;
float wagePerHour;
  
public:
Employee(string aName) : name(aName), wagePerHour(MIN_WAGE_PER_HOUR) { }

Employee(string aName, float aWagePerHour) : name(aName) {
if (aWagePerHour < MIN_WAGE_PER_HOUR) {
aWagePerHour = MIN_WAGE_PER_HOUR;
}
wagePerHour = aWagePerHour;
}

string getName() { return name; }

virtual void display() { cout << name << endl; }

virtual void payroll() {
cout << name << " $" << fixed << setprecision(2) << (wagePerHour * NUM_WORK_HOURS) << endl;
}
};

#endif // Included_Employee_H

File: Cashier.h

#ifndef Included_Cashier_H
#define Included_Cashier_H

#include <iostream>

#include "Employee.h"

using namespace std;

class Cashier : public Employee
{
public:
Cashier(string aName, float aWagePerHour) : Employee(aName, aWagePerHour) { }

virtual void display() {
cout << name << " (Cashier hourly wage: $" << fixed << setprecision(2) << wagePerHour << ")" << endl;
}
};

#endif // Included_Cashier_H

File: Manager.h

#ifndef Included_Manager_H
#define Included_Manager_H

#include <iostream>

#include "Employee.h"

using namespace std;

class Manager : public Employee
{
private:
int managerId;
float wagePerWeek;

public:
Manager(string aName, int aManagerId, float aWagePerWeek) : Employee(aName), managerId(aManagerId) {
wagePerWeek = aWagePerWeek;
}

virtual void display() {
cout << name << " (Manager id: " << managerId << ", weekly wage: $" << fixed << setprecision(2) << wagePerWeek << ")" << endl;
}

virtual void payroll() {
cout << name << " $" << fixed << setprecision(2) << wagePerWeek << endl;
}
};

#endif // Included_Manager_H

File: project6-main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> // for exit()
#include <iomanip> // fixed, setprecision()
#include <list> //for std::list

#include "Employee.h"
#include "Cashier.h"
#include "Manager.h"

using std::string;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::getline;
using std::ifstream;
using std::fixed;
using std::setprecision;

bool DEBUG = false; // toggles extra printing

const string DEFAULT_INPUT_FILENAME = "project6-testA.tab";

// Action characters in the input file
const char ADD_EMPLOYEE = 'E';
const char ADD_CASHIER = 'C';
const char ADD_MANAGER = 'M';
const char REMOVE = 'R';
const char PAYROLL = 'P';
const char FIND = 'F';
const char DISPLAY_ALL = 'D';

void displayAll(list<Employee *> & employees) {
cout << "Displaying " << employees.size() << (employees.size() == 1 ? " employee:" : " employees:") << endl;

for (list<Employee *>::iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr) {
(*employeeItr)->display(); // polymorphic method
}

cout << endl;
}

void payroll(list<Employee *> & employees) {
cout << "Weekly payroll:" << endl;

for (list<Employee *>::iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr) {
(*employeeItr)->payroll(); // polymorphic method
}

cout << endl;
}

Employee* getEmployee(list<Employee *> & employees, string name) {
for (list<Employee *>::iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr) {
if ((*employeeItr)->getName().compare(name) == 0) {
return (*employeeItr);
}
}

return NULL;
}

void find(list<Employee *> & employees, string name) {
Employee *employee = getEmployee(employees, name);

if (employee != NULL) {
employee->display();
} else {
cout << "Unable to find " << name << endl;
}
}

void remove(list<Employee *> & employees, string name) {
Employee *employee = getEmployee(employees, name);

if (employee != NULL) {
employees.remove(employee);
} else {
cout << "Unable to remove " << name << endl;
}
}

// Process the inputs in inputFilename
void processInputs( string inputFilename ){

// declare an STL container named employees here
list<Employee *> employees;
  
// open file
ifstream fileStream;
fileStream.open( inputFilename.c_str() );

// verify the file is opened correctly
if( ! fileStream ){
cerr << "ERROR: Can not open input file " << inputFilename << "!" << endl;
exit(1);
}

cout << "Importing instructions from " << inputFilename << " ..." << endl;
  
char action = '';
  
// while there's more patients to process
// read in the action and make sure that we're not at the end of the file
while( fileStream >> action ){
if( DEBUG ){ cout << "action: " << action << endl; }
string name;
switch( action ){

case ADD_EMPLOYEE:
// get the employee's name from the file
fileStream >> name;
cout << "Adding employee " << name << endl;

employees.push_back(new Employee(name));

cout << endl;
if( DEBUG ){ displayAll( employees); }
break;
case ADD_CASHIER:
// get the employee's name from the file
fileStream >> name;
float hourlyWage;
fileStream >> hourlyWage;
cout << "Adding cashier " << name << endl;

employees.push_back(new Cashier(name, hourlyWage));

cout << endl;
if( DEBUG ){ displayAll( employees); }
break;
case ADD_MANAGER:
// get the employee's name from the file
fileStream >> name;
int id;
fileStream >> id;
float salary;
fileStream >> salary;
cout << "Adding manager " << name << endl;

employees.push_back(new Manager(name, id, salary));

cout << endl;
if( DEBUG ){ displayAll( employees); }
break;
case REMOVE:
// get the employee's name from the file
fileStream >> name;
cout << "Removing " << name << endl;

remove(employees, name);

cout << endl;
break;
case PAYROLL:
payroll( employees );
break;
case FIND:
// get the employee's name from the file
fileStream >> name;
cout << "Finding " << name << endl;

find(employees, name);

cout << endl;
break;
case DISPLAY_ALL:
displayAll( employees );
break;
default:
cerr << "ERROR: Unknown action " << action << "!" << endl;
exit(1);
}
}

// close the file
fileStream.close();
}


int main( /*int argc, char* argv[] */){

// If just a return (' ') is entered, then use DEFAULT_INPUT_FILENAME.
// Otherwise, read in the input filename from STDIN.
string inputFilename;
cout << "Please enter the input filename (or simply press return to use " << DEFAULT_INPUT_FILENAME << ") ";
getline( cin, inputFilename);
if( inputFilename == ""){
inputFilename = DEFAULT_INPUT_FILENAME;
}

// process transactions in the file
processInputs( inputFilename );
  
return 0;
}

Sample Test Data File: project6-testA.tab

D
P
F Sophia
R Sophia
E Sophia
D
P
F Sophia
R Sophia
D
F Sophia
R Sophia
C Jackson 77.91
M Emma 123456789 1234.56
E Aiden
C Olivia 42.87
M Lucas 987654321 7890.12
P
D
R Jackson
R Emma
R Aiden
R Olivia
R Lucas
D

Compilation: g++ project6-main.cpp

Sample Execution Output: ./a.out
Please enter the input filename (or simply press return to use project6-testA.tab)

Importing instructions from project6-testA.tab ...
Displaying 0 employees:

Weekly payroll:

Finding Sophia
Unable to find Sophia

Removing Sophia
Unable to remove Sophia

Adding employee Sophia

Displaying 1 employee:
Sophia

Weekly payroll:
Sophia $290.00

Finding Sophia
Sophia

Removing Sophia

Displaying 0 employees:

Finding Sophia
Unable to find Sophia

Removing Sophia
Unable to remove Sophia

Adding cashier Jackson

Adding manager Emma

Adding employee Aiden

Adding cashier Olivia

Adding manager Lucas

Weekly payroll:
Jackson $3116.40
Emma $1234.56
Aiden $290.00
Olivia $1714.80
Lucas $7890.12

Displaying 5 employees:
Jackson (Cashier hourly wage: $77.91)
Emma (Manager id: 123456789, weekly wage: $1234.56)
Aiden
Olivia (Cashier hourly wage: $42.87)
Lucas (Manager id: 987654321, weekly wage: $7890.12)

Removing Jackson

Removing Emma

Removing Aiden

Removing Olivia

Removing Lucas

Displaying 0 employees:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote