Inheritance- intro
The following topics are covered in this section:
Inheritance means getting something from the parent. In C++ also, inheritance means the same. But who is the parent?
Remember that classes and objects are a key feature in C++. When we talk of inheritance in C++, we refer to classes. We will have the parent class and the derived class. The derived class inherits all properties of the parent class. The parent class is also known as the base class. All classes that arise from the base class are known as derived classes. These derived classes inherit all non-private parts of the base class (i.e. there are a few things that cannot be inherited from the base class. This is discussed later).
We have discussed earlier about a general class called ‘bacteria’. We also referred to two other classes called Ecoli and TB. Now, bacteria will form the base class while ecoli and TB will be derived from the bacteria class. In this way they will inherit all the general properties of a bacteria. In addition they will have their own special features (in our case they would have a special member function to describe the unique way in which they move).
You might wonder as to whether the child inherits everything from the parent? The derived class inherits everything that is NOT PRIVATE in the parent class. Remember we used ‘private’ and ‘public’ to restrict access to class members. Now when you declare any member as ‘private’, that member cannot be inherited by any of the children (in other words, private members are not inheritable). Only the ‘public’ members are inheritable.
When any data is made private in a class, it can be accessed
ONLY by member functions of that class. A derived class CANNOT access the
private members of its base class.
It might seem as if the concept of data encapsulation would be lost if a derived
class can access only it’s parent’s public members (because the aim of classes
is to restrict access to data and hence we make them public. But if we can’t
inherit the private data, what’s the use of inheritance?). C++ provides us with
a third access specifier called ‘protected’.
The three access specifiers for class members are:
If you declare class members as ‘protected’, then any class derived from the parent can access the protected members but no one from outside can make use of the protected members. In a way protected members are similar to private class members (because both cannot be accessed from outside the class). But both are different because protected members can be accessed by derived classes whereas private members cannot be accessed by derived classes. The use of protected access specifier is very important in inheritance.
Let's take a look at the syntax for making a derived class.
Create the base class as you normally would. Then when you want to create the derived class, use the following syntax:
class d1 : public b1
{
//body of d1;
};
where d1 is the name of the derived class and b1 is the name of the base class.
Let’s consider a different example for inheritance (instead of bacteria and biology). Consider cars: there are different types of cars in the world. Broadly we could divide them into two categories: normal cars and racing cars. Both of these categories belong to the same family of cars (since all cars will have some properties in common). Though they are common in certain respects they will have their own properties as well. Thus these two categories of cars can be derived from the general class of cars. Again within racing cars we have different manufacturers (and each manufacturer might make use of different specifications). For instance a ‘Ferrari’ and a ‘McLaren’ are not one and the same (even though both are racing cars). Thus let’s model a general class called ‘car’ (which will refer to racing cars). We will provide this class with only two properties: color and speed. From this class, we shall derive two classes called ‘Ferrari’ and ‘McLaren’.
From the above figure, it can be said that, “an object of type Ferrari is a RacingCar”. This is referred to as “is-a” relationship. The various types of relationships are:
Remember: Every Ferrari is a car but not every car is a Ferrari.
When designing relationships between classes, ideally, whatever the parent can do the child should be able to do (this is the ‘is-a’ relationship).
Program to demonstrate inheritance//
Protected data will be inherited by the derived classes. Both the member data
(speed and color) of the base class ‘car’ are protected. Thus all classes
derived from ‘car’ will also have the property of ‘speed’ and ‘color’.
You might be wondering as to how the constructors and destructors will execute
and in what order?
The output would be:
Creating a Car
Creating a Ferrari
Enter the colour of your car : red
Enter the top speed : 150
Destroying the Ferrari
Destroying Car
As can be seen, constructors execute from the top to bottom while the destructors execute in the reverse order. It’s like you will first construct a car and then label it as a Ferrari and while destroying you would remove the label Ferrari first and then dismantle the car.
Remember: The constructor of the base class and also the destructor will be invoked.
More on Inheritance (Base-Class Access specifier)
So far we have used the following syntax for inheritance:
class derived-name: public base-name
{
//body of class;
};
where derived-name is the name of the derived class and
base-name is the name of the base class.
We have already dealt with access specifiers as used for class members. The type
of access specifier used determines how the members of a class are accessed
(i.e. whether they can be accessed from outside, or whether their access is
restricted to class members or whether they can be accessed by derived classes).
The 3 types of access specifiers are:
These 3 access specifiers can be used to determine how the derived class will access the properties derived from the base class. The general syntax for inheritance is thus as given below:
class name-of-derived-class : access-specifier name-of-base-class
{
//body of derived class;
};
We know that the derived class cannot access the private members of the base class. But what about the protected and public members of the base class? Do they become private members of the derived class? It is for clarifying this that we have to use an access-specifier to specify what we want the inherited members to become.
Go back to the Contents Page 2
Copyright © 2004 Sethu Subramanian All rights reserved.