Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 using namespace std;

class Employee { char name[20]; public: //constructors

// virtual function virtual void show_info (){ cout << "Employee" << endl; } };

The steps to be taken are: 1. 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];

2.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.

3.Add a public member function to the base class (Employee) so that it returns the name of the employee:

char *getName() { return name; }

4.Build all classes so that they only have to assign the name to the inherited variable. So far they wont have other variables.

5.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.

6.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;

7. Now run and compile.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

Students also viewed these Databases questions

Question

=+Describe an important trade-off you recently faced

Answered: 1 week ago