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.
A SIMPLE CLASS TO START WITH
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //vehicle.cpp // vehicle header file #ifndef VEHICLE_H #define VEHICLE_H class vehicle { protected: int wheels; float weight; public: void initialize(int in_wheels, float in_weight); int get_wheels(void); float get_weight(void); float wheel_loading(void); }; #endif |
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
THE IMPLEMENTATION FOR VEHICLE
Examine the file named VEHICLE.CPP and you will find that it is the implementation of the vehicle class.
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 | // vehicle.cpp #include "vehicle.h" // initialize to any data desired void vehicle::initialize(int in_wheels, float in_weight) { wheels = in_wheels; weight = in_weight; } // get the number of wheels of this vehicle int vehicle::get_wheels() { return wheels; } // return the weight of this vehicle float vehicle::get_weight() { return weight; } // return the weight on each wheel float vehicle::wheel_loading() { return weight/wheels; } |
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.
USING THE VEHICLE CLASS
The file named TRANSPRT.CPP uses the vehicle class in exactly the same manner as we illustrated in the last chapter.
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 | // transport.cpp #include <iostream.h> #include "vehicle.h" main() { vehicle car, motorcycle, truck, sedan; car.initialize(4, 3000.0); motorcycle.initialize(2, 900.0); truck.initialize(18, 45000.0); sedan.initialize(4, 3000.0); cout << "The car has " << car.get_wheels() << " wheels.n"; cout << "The truck has a loading of " << truck.wheel_loading() << " pounds per wheel.n"; cout << "The motorcycle weighs " << motorcycle.get_weight() << " pounds.n"; cout << "The sedan weighs " << sedan.get_weight() << " pounds, and has " << sedan.get_wheels() << " wheels.n"; } // Result of execution // // The car has 4 wheels. // The truck has a loading of 2500 pounds per wheel. // The motorcycle weighs 900 pounds. // The sedan weighs 3000 pounds, and has 4 wheels. |
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