Question: Lab 12 1. Introduction This weeks lesson is focused on Polymorphism, for which you will create classes using inheritance and virtual functions. To start with,
Lab 12
1. Introduction
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 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.
1.1 Exercise 1
This exercise deals with using virtual functions, seeing how they act according to the object on
which it is applied, which you will see in main(). Create a file lab12p1.cpp and enter the following
code:
class Employee
{
protected: double sal; //salary base
public: Employee(double s){ sal=s;}
double Payment(){ return sal;}
void prt(){
cout << "Salary="<< Payment() < class Manager : public Employee { double inc; public: Manager(double s, double i) : Employee(s) { inc = i; } double Payment(){ return sal*inc; } }; void main() { Employee e1(1500); Manager m1(1500, 1.5); cout << Exercise about inheritance and polymorphism< e1.prt( ); m1.prt( ); } Compile the project and run it, observing what is printed on the console. Next, add the word virtual before the function Payment() in the base class and run again the program. What happens this time? Why? Turn in your cpp file and a txt file called lab12p1.txt with your discussion form above to the Lab 12 folder in BB Learn. 1.2 Exercise 2 For this exercise you must create another project, called Lab12p2.cpp, which again shows the class hierarchy and a new declaration of the base class Employee, which will be used as a starting point: #include class Employee { char name[20]; public: //constructors // virtual function virtual void show_info (){ cout << "Employee" << endl; } }; The steps to be taken are: Enter in the base class the member variable that contains the employee's name. This variable will be private so that derived classes cannot access that value. char name[20]; Create the constructors needed to be able to indicate the name. The constructors of the base class are the only ones that, using the strcpy() function, will assign its value to the name variable. The remaining constructors must call the respective constructor of their base class. Add a public member function to the base class (Employee) so that it returns the name of the employee: char *getName() { return name; } Build all classes so that they only have to assign the name to the inherited variable. So far they wont have other variables. Redefine show_info() function in all other derived classes so it writes the name of the employee. You must use the getName() function to get the name, since the variable is private. Add the following code to the main function: Employee Rafa("Rafa"); Manager Mario("Mario"); Worker Anton("Anton"); Officer Luis("Luis"); Technician Pablo("Pablo"); // The type of object pointed by a pointer to the base class determines the function that is being called Employee *pe; cout << " Inheritance and Polymorphism: " << endl; pe = &Rafa; pe->show_info(); pe = &Mario; pe-> show_info(); pe = &Anton; pe-> show_info(); pe = &Luis; pe-> show_info(); pe = &Pablo; pe-> show_info(); cout << "Ya he terminado." << endl; Now compile and run the program. Watch what happens. Then delete the word virtual in the declaration of the show_info() function in the base class Employee, observing the difference in the output of the program. This is a clear example of polymorphism. Once you understand the difference, reinsert the virtual keyword and go to next exercise. Before moving on be sure to turn in your Lab12p2.cpp file to the BB Learn folder. 1.3 Exercise 3 The objective of this exercise is to add a member variable, in order to distinguish the management of private member variables with regard to protected. In addition, you must create a function that displays all the information available about an employee. For that, you must start from the previous exercise and modify it appropriately. To avoid copying all the files above, we will make a copy of Lab12p2.cpp and call it Lab12p3.cpp. The changes to be made are: Enter a member variable in the base class that contains the employee's salary. This variable will be private so that derived classes cant access its value. long salary; Add member variable degree to the class Manager. This variable is a pointer to a string of characters (using dynamic memory allocation), and it can be defined as protected. char *degree; Add the member variable position to the class Worker. This variable is a pointer to a string of characters (using dynamic memory allocation) and it can be defined as protected so that classes derived from the class Worker can access it. char *position; Create the necessary constructors to be able to indicate the salary, the degree (in case it is a manager) or the position in the case of a worker. Create the public member function GetSalary() in the base class Employee, which returns the salary of each employee. long GetSalary(); The virtual function show_info() must display on the screen all the information stored for each employee: name, salary, and also their position or degree, depending on whether it is a worker or a manager. In the main() program an array of 6 pointers to employees is going to be created. Previously, the number of employees will be defined with a define: Then, in the same file, you must create the employees by calling to each of the constructors. #define NUM_EMPLOYEES 6 Employee* EmployeeList[NUM_EMPLOYEES]; EmployeeList[0] = new Manager("Carla Garcia", 35000, "Economist"); EmployeeList[1] = new Manager ("Juan Perez", 38000, "Ingineer"); 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"); Finally, after creating the objects, the show_info() function of each employee should be called, to show their information on screen. for(int i=0;i EmployeeList[i]->show_info(); Compile this code and note the output. Make sure you understand the use of the public, private and protected keywords as well as the virtual functions. Upload the lab12p3.cpp file to BB Learn 1.4 Exercise 4 After testing the correct operation of the previous exercise, 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). First copy the lab12p3.cpp file to lab12p4.cpp and then 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. Then upload the finished lab12p4.cpp file BB Learn.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
