How to start an OOP Project

The observant student will notice that we begin our use of object oriented programming by identifying an object, or in this case a class of objects and even some subordinate objects, which we completely define. When we get to the main program we then have a simple job with the remaining needs and they are completed using standard procedural programming techniques which we are familiar with. This is the way to begin any object oriented programming project, by first identifying a few objects that can be separated conveniently from the rest of the code, programming them, then writing the main program. It should be added that, for your first project using objects, do not try to make everything an object. Select a few objects and after gaining experience with object oriented programming techniques, use more objects on future projects. Most programmers use too many objects for their first project and write very obtuse, unreadable code.

The person header file

Examine the file named person.h for the definition file for the person class. This class definition should cause you no problem to understand since there is nothing new here. The only thing that should be mentioned about this class is that the protected mode is used for the variables so that they are readily available in the derived classes which will inherit this class. The person.h header file uses #ifndef and #define directives to define the person class so that it can not be defined multiple times..

The person implementation

The implementation for the person class is given here and it is a little strange in the way it is written and used. The intent of this program is that the virtual method named display() in this file will never be used, but it is required by the C++ compiler to be used for a default in case some of the subclasses do not have this function available. In the main program we will be careful to never call this function due to the nature of the program we are writing. Keep in mind that C++ requires an implementation of all virtual functions even if they are never used. In this case the message is obviously intended to be output as an error message.

Be sure to compile this program prior to going on to the next class definitions.

The supervisor header

The file named SUPERVSR.H contains the class definitions for the three derived classes, supervisor, programmer, and secretary. These were all placed in a single file for two reasons. The first reason is to simply illustrate to you that this can be done, and secondly, to allow some of the files to be combined on the disk and to require fewer compilations by you prior to executing the resulting program. This is actually a good way to combine these files since they are all derived classes of a common class. It is a matter of style or personal taste.

You will notice that all three of these classes contain a method named display() and all have the same return value of void, and all have the same number of parameters as the parent class’s method of the same name. All of this equality is required because they will all be called by the same call statement. You will also notice that the other method in each class has the same name, but different numbers and types of formal parameters which prevents this method from being used as a virtual method.

The remainder of this file is simple and you should be able to read the code and understand it completely. Once again, this file cannot be compiled or executed.