Question
This weeks lesson is focused on Polymorphism, for which you will create classes using inheritance and virtual functions. To start with, what is polymorphism? Polymorphism
This week’s lesson is focused on Polymorphism, for which you will create classes using inheritance and virtual functions. To start with, what is polymorphism? Polymorphism allows the name of a function to invoke a response on a base-class-type object and a different one in objects of a derived class, for which virtual functions are used.
After testing the correct operation of the previous exercise, provided below, you have to modify the program to ask the user the number of employees to be created and to request all data from the newly created object (name, salary, position or degree, etc. depending on the type of object to be created). Make the changes below:
You must make the main program to have a menu with the following options:
1. 1. Choose what type of employee to create: Manager, Officer or Technician.
2. 2. Make a list of all the employees introduced.
3. 3. Search for a specific employee by name, and
4. 4. Exit from the program.
Remember that all objects should be stored in the list EmployeeList and it must have a unique variable counter to keep track of the objects created. Test that your program works correctly.
Code from previous exercise:
#include
#include
using namespace std;
class Employee
{
private:
char name[20];
long salary;
public:
//constructors
// virtual function
virtual void show_info()
{
this->show_info();
}
char const * getName()
{
return name;
}
long GetSalary()
{
return salary;
}
Employee(char const *empName,long sal)
{
salary = sal;
strcpy(name, empName);
}
};
class Manager : public Employee
{
protected:
char const *degree;
public:
Manager(char const *name,long sal, char const *d) : Employee(name,sal)
{
degree = d;
}
void show_info()
{
cout << "Manager: " << getName() << endl;
cout << "degree: " << degree << endl;
cout << "salary: " << this->GetSalary() << endl;
}
};
class Worker : public Employee
{
protected:
char const *position;
public:
Worker(char const *name,long sal,char const *pos) : Employee(name,sal)
{
position = pos;
}
void show_info()
{
cout << "Worker: " << getName() << endl;
cout << "postion: " << position << endl;
cout << "salary: " << this->GetSalary() << endl;
}
};
class Officer : public Worker
{
public:
Officer(char const *name, long sal, char const *pos) : Worker(name,sal,pos)
{
}
void show_info()
{
cout << "Officer: " << getName() << endl;
cout << "postion: " < cout << "salary: " << this->GetSalary() << endl;
}
};
class Technician : public Worker
{
public:
Technician(char const *name, long sal, char const *pos) : Worker(name, sal, pos)
{
}
void show_info()
{
cout << "Technician: " << getName() << endl;
cout << "postion: " << position << endl;
cout << "salary: " << this->GetSalary() << endl;
}
};
int main()
{
#define NUM_EMPLOYEES 6
Employee* EmployeeList[NUM_EMPLOYEES];
EmployeeList[0] = new Manager ("Carla Garcia", 35000, "Economist");
EmployeeList[1] = new Manager ("Juan Perez", 38000, "Engineer");
EmployeeList[2] = new Officer("Pedro Egia", 18000, " Officer 1");
EmployeeList[3] = new Officer("Luisa Penia", 15000, " Officer 2");
EmployeeList[4] = new Technician("Javier Ramos", 19500, "Welder");
EmployeeList[5] = new Technician("Amaia Bilbao", 12000, "Electricist");
for (int i = 0; i EmployeeList[i]->show_info();
return 0;
}
Step by Step Solution
3.48 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
include include using namespace std class Employee private char name20 long salary public constructors virtual function virtual void showinfo thisshow...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started