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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | // supervsr.cpp #include "supervsr.h" #include <iostream.h> #include <stdio.h> #include <string.h> // In all cases, init_data assigns values to the class variables and // display outputs the values to the monitor for inspection. void supervisor::init_data(char in_name[], int in_salary, char in_title[]) { strcpy(name,in_name); salary = in_salary; strcpy(title, in_title); } void supervisor::display(void) { cout << "Supervisor --> " << name << "'s salary is " << salary << " and is the " << title << ".\n\n"; } void programmer::init_data(char in_name[], int in_salary, char in_title[], char in_language[]) { strcpy(name,in_name); salary = in_salary; strcpy(title, in_title); strcpy(language, in_language); } void programmer::display(void) { cout << "Programmer --> " << name << "'s salary is " << salary << " and is " << title << ".\n"; cout << " " << name << "'s specialty is " << language << ".\n\n"; } void secretary::init_data(char in_name[], int in_salary, char in_shorthand, char in_typing_speed) { strcpy(name,in_name); salary = in_salary; shorthand = in_shorthand; typing_speed = in_typing_speed; } void secretary::display(void) { cout << "Secretary ---> " << name << "'s salary is " << salary << ".\n"; cout << " " << name << " types " << typing_speed << " per minute and can "; if (!shorthand) cout << "not "; cout << "take shorthand.\n\n"; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | // employee.cpp #include <iostream.h> #include "person.h" #include "supervsr.h" person *staff[10]; main() { supervisor *suppt; programmer *progpt; secretary *secpt; cout << "XYZ Staff -- note salary is monthly.\n\n"; suppt = new supervisor; suppt->init_data("Big John", 5100, "President"); staff[0] = suppt; progpt = new programmer; progpt->init_data("Joe Hacker", 3500, "debugger", "Pascal"); staff[1] = progpt; progpt = new programmer; progpt->init_data("OOP Wizard", 7700, "senior analyst", "C++"); staff[2] = progpt; secpt = new secretary; secpt->init_data("Tillie Typer", 2200, 1, 85); staff[3] = secpt; suppt = new supervisor; suppt->init_data("Tom talker", 5430, "Sales manager"); staff[4] = suppt; progpt = new programmer; progpt->init_data("Dave Debugger", 5725, "code maintainer", "assembly language"); staff[5] = progpt; for (int index = 0 ; index < 6 ; index++ ) staff[index]->display(); cout << "End of employee list.\n"; } // Result of execution // XYZ Staff -- note salary is monthly. // // Supervisor --> Big John's salary is 5100 and is the President. // // Programmer --> Joe Hacker's salary is 3500 and is debugger. // Joe Hacker's specialty is Pascal. // // Programmer --> OOP Wizard's salary is 7700 and is senior analyst. // OOP Wizard's specialty is C++. // // Secretary ---> Tillie Typer's salary is 2200. // Tillie typer types 85 per minute and can take shorthand. // // Supervisor --> Tom Talker's salary is 5430 and is the sales manager. // // Programmer --> Dave Debugger's salary is 5725 and is code maintainer. // Dave Debugger's specialty is assembly language. // // End of employee list. |
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.