In Object Oriented Programming, Inheritance is the process by which objects of one class acquire the properties and functionality of objects of another class. It supports the concept of hierarchical classification. For example, the bird robin is a part of the class flying bird which is again a part of the class bird.

One reason to use inheritance is that it allows you to reuse code from a previous project but gives you the flexibility to slightly modify it if the old code doesn’t do exactly what you need for the new project. It doesn’t make sense to start every new project from scratch since some code will certainly be repeated in several programs and you should strive to build on what you did previously. Moreover, it is easy to make an error if we try to modify the original class, but we are less likely to make an error if we leave the original alone and only add to it. Another reason for using inheritance is if the project requires the use of several classes which are very similar but slightly different.

In this tutorial we will concentrate on the mechanism of inheritance and how to build it into a program. A better illustration of why you would use inheritance will be given in later tutorials where we will discuss some practical applications of object oriented programming.

The principle of inheritance is available with several modern programming languages and is handled slightly differently with each. C++ allows you to inherit all or part of the members and methods of a class, modify some, and add new ones not available in the parent class. You have complete flexibility, and as usual, the method used with C++ has been selected to result in the most efficient code execution.


Examine the file named vehicle.h for a simple class which we will use to begin our study of inheritance in this chapter. The header file uses #ifndef and #define directives to define the class.

There is nothing unusual about this class header, it has been kept very simple. It consists of four simple methods which can be used to manipulate data pertaining to our vehicle. What each method does is not especially important at this time. We will eventually refer to this as a base class or parent class, but for the time being, we will simply use it like any other class to show that it is indeed identical to the classes already studied. Note that we will explain the added keyword protected shortly.

Ignore lines 4, 5, and 17 until the end of this chapter where they will be explained in detail. This file cannot be compiled or executed because it is only a header file


Examine the file named VEHICLE.CPP and you will find that it is the implementation of the vehicle class.

The initialize() method assigns the values input as parameters to the wheels and weight variables. We have methods to return the number of wheels and the weight, and finally, we have one that does a trivial calculation to return the loading on each wheel. We will have a few examples of methods that do some significant processing later, but at this point, we are more interested in learning how to set up the interface to the classes, so the implementations will be kept trivial.

As stated above, this is a very simple class which will be used in the next program. Later in this tutorial we will use it as a base class. You should compile this class at this time in preparation for the next example program, but you cannot execute it because there is no entry point.


The file named TRANSPRT.CPP uses the vehicle class in exactly the same manner as we illustrated in the last chapter.

This should be an indication to you that the vehicle class is truly nothing more than a normal class as defined in C++. We will make it a little special, however, by using it unmodified as a base class in the next few example files to illustrate inheritance. Inheritance uses an existing class and adds functionality to it to accomplish another, possibly more complex job.

You should have no problem understanding the operation of this program. It declares four objects of the vehicle class, initializes them, and prints out a few of the data points to illustrate that the vehicle class can be used as a simple class because it is a simple class. We are referring to it as a simple class as opposed to calling it a base class or derived class as we will do shortly.

If you thoroughly understand this program, you should compile and execute it, remembering to link the vehicle object file with this object file