The Supervisor Implementation

The file named SUPERVSR.CPP contains the implementation for the three classes. If you spend a little time studying the code, you will find that each of the methods named init_data() simply initializes all fields to those passed in as the actual arguments in a very simple manner.

The method named display(), however, outputs the stored data in different ways for each class since the data is so different in each of the classes. Even though the interface to these three methods is identical, the actual code is significantly different. There is no reason code besides output could not have been used, but the output is so visible when the program is executed that it was chosen for this illustration.

This file should be compiled at this time in preparation for the next example program which will use all four classes as defined in these four files.

The First Calling Program

The file named EMPLOYEE.CPP is the first program that uses the classes developed in this chapter, and you will find that it is a very simple program.

We begin with an array of ten pointers, each pointing to the base class. As you recall from the last chapter, this is very important when using virtual functions, the pointer must point to the base class. The pointers that will be stored in this array will all point to objects of the derived classes however. When we use the resulting pointers to refer to the methods, the system will choose the method at run time, not at compile time as nearly all of our other programs have been doing.

We allocate six objects in lines 16 through 39, initialize them to some values using the methods named init_data(), then assign the pointers to the members of the array of pointers to person. Finally, in lines 41 and 42, we call the methods named display() to display the stored data on the monitor. You will notice that even though we only use one method call in line 42, we actually send messages to each of the three methods named display() in the subclasses. This is true dynamic binding because if we were to change the values of some of the pointers in the array, we would then call different methods with the same pointers.

Be sure to compile and execute this program before continuing on in this chapter. You will recall that the linking step requires you to combine several files in order to satisfy all system calls. After you have done that, we will use the same objects in another way to show how they can be reused.

Share
Tweet
Share
Pin