We promised to return to the strange looking preprocessor directive in lines 4, 5 and 17 in the VEHICLE.H file, and this is the time for it. When we define the derived class car, we are required to supply it with the full definition of the interface to the vehicle class since car is a derived class of vehicle and must know all about its parent. We do that by including the vehicle class into the car class, and the car class can be compiled. The vehicle class must also be included in the header file of the truck class for the same reason.

When we get to the main program, we must inform it of the details of all three classes, so all three header files must be included as is done in lines 3 through 5 of ALLVEHIC.CPP, but this leads to a problem. When the preprocessor gets to the car class, it includes the vehicle class because it is listed in the car class header file, but since the vehicle class was already included in line 3 of ALLVEHIC.CPP, it is included twice and we attempt to redefine the class vehicle. Of course it is the same definition, but the system doesn’t care, it simply doesn’t allow redefinition of a class. We allow the double inclusion of the file and at the same time prevent the double inclusion of the class by building a bridge around it using the word VEHICLE_H. If the word is already defined, the definition is skipped, but if the word is not defined,

the definition is included and the word is defined at that time. The end result is the actual inclusion of the class only once, even though the file is included more than once. You should have no trouble understanding the logic of the includes if you spend a little time studying this program sequence.

Even though ANSI-C allows multiple definitions of entities, provided the definitions are identical, C++ does not permit this. The primary reason is because the compiler would have great difficulty in knowing if it has already made a constructor call for the redefined entity, if there is one. A multiple constructor call for a single object could cause great havoc, so C++ was defined to prevent any multiple constructor calls by making it illegal to redefine any entity. This is not a problem in any practical program.

The name VEHICLE_H was chosen as the word because it is the name of the file, with the period replaced by the underline. If the name of the file is used systematically in all of your class definitions, you cannot have a name clash because the filename of every class must be unique. It would be good for you to get into the practice of building the optional skip around all of your classes. All class definition files in the remainder of this tutorial will include this skip around to prevent multiple inclusions and to be an example for you. You should get into the practice of adding the skip around to all of your class headers no matter how trivial they may seem to be.


Continuing where we started in chapter 5, we will inherit the date class into the file named NEWDATE.H and add a member variable and a new method to the class. Actually, this is not a good way to add the day_of_year to the date class since it is available in the structure returned from the system call in the date class. However, we are more interested in illustrating inheritance in a practical example than we are in developing a perfect class, so we will live with this inefficiency. You will note that we add one variable and one method to create our new class.

The program named NEWDATE.CPP contains the implementation for the added method and should be simple for the student to understand. This class implementation uses the array days[] from the date class implementation since it was defined as a global variable there. The method named get_time_of_day() involves very simple logic but still adjusts for leap years.

Finally, the example program named TRYNDATE.CPP will use the new class in a very simple way to illustrate that the derived class is as easy to use as the base class and in fact the main program has no way of knowing that it is using a derived class.

You should compile and link this program to gain the experience of doing so. Remember that it will be necessary to link in the object code for the original date class as well as the object code from the newdate class and the main program.