Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Inheritance Types With Examples in C++

1. Introduction

In this article, we will see about Inheritance in C++ with Simple example Program. Inheritance is code re-use mechanism. For example, we will think about truck and car. They both have the functionality of speed up, brake, starting and stopping the engine. They both have properties like fuel level, current speed, etc. But, most of the cars have 4 tires and truck may have six, eight or even 16 make you think Number of tires property may not be same for truck and car.

From the above argument of real-world items, we can put

CVehicle

as base class which holds common properties like Fuel Level and Current Speed. Similarly, the common action it can perform like accelerate and brake can be placed in

CVehicle

.

CCar

and

CTruck

can derive from the

CVehicle

. These classes have properties and behavior unique to it. Say for example both

CCar

and

CTruck

can have member variable to hold Number of tires as they vary between car and truck. This member is not an eligible property to fit in

CVehicle

.

Have a Look at below picture:

Inheritance Relation between Vehicle, Car, Truck

Inheritance Relation between Vehicle, Car, Truck

2. The CVehicle Class

The below code shows

CVehicle

class implementation. This class has two private members

m_FuelLevel

and

m_CurrentSpeed

. The class member functions

Accelerate()

and

Brake()

access these member variables. In these functions, we change the member variables and also display the state of the vehicle in terms of Speed.

3. Inheritance Syntax: The CCar Class

The syntax of the inheritance can be understood from the following picture:

Public Inheritance

Public Inheritance

The above picture shows that

CCar

is the new class we will create and this class will inherit common properties and behavior from existing class

CVehicle

. Remember, we created the

CVehicle

class in the previous section. In our example,

CCar

is the class ‘Derived’ from the existing ‘Base’ class

CVehicle

. The public keyword encircled here shows the mode of the inheritance. The mode of the inheritance here is public. We will look at other modes of inheritance in the later sections.

Now have a look at the code given below:

The derived class

CCar

inherits the properties and methods from the base class

CVehicle

. The

CCar

class has a new member called m_trunk which is specific to it. This new member

m_trunk

is used to specify whether a car has a storage space or not. Note that the base class properties

m_CurrentSpeed

,

m_FuelLevel

 are accessed here on the derived class as they are inherited and available. In public inheritance, the derived class can access all the public and protected members of the base class.

5. The CTruck Class Inheritance

The Truck class also derived from the

CVehicle

class and uses the common properties and behaviors defined in the

CVehicle

class. But, comparing with the

CCar

it does not care about the Trunk and in place of that it deals with the property like crane type and crane length. The below code shows the

CTruck

class implementation:

Note that in both

CCar

and

CTruck

classes, the print function uses the base class version to display the properties. For Example, making a call to

PrintTruckProps()

will make a call to the base class version of the print method say

PrintVehicleProps

in our case. In the program main, we tested these inherited classes. The code snippet and the output is shown below:

Output Of Program Example

Output Of Program Example

The Complete Example code is given below:

6. Access Specifiers in Inheritance

We know that the access to a class’s data members and member functions are controlled by the Access Specifiers. The three access specifiers are private, public and protected. Derived class can access the protected member of the base class. Whereas, non-derived classes and global function are restricted to access these protected members. Below diagram shows the access specifiers impact over the program structure.

Access Members In Inheritance

Access Members In Inheritance: Role of Access Specifiers

Red arrow shows that only class member functions can access the private data members and private member functions. Derived class doesn’t have access to private members of the base class. We can access public member anywhere when we have the object of the class. The point one should note here is that protected scope of a class is meant it can be accessed by inherited class but not by the outside world.

7. Private, Public, Protected Inheritance

All examples (Both car and truck inheritance) of this article shows public inheritance. In the case of public inheritance (Refer the encircled one in the picture at section 3) the members are inherited as it is. That is, in the derived class,

  1. Base class Public and protected members are inherited as it is.
  2. Private members are not inherited.

We can perform private inheritance by replacing the public as a private in the inheritance syntax. And, for protected inheritance replace public as protected.

In case of a protected inheritance, in the derived class,

  1. Base class public members are protected members.
  2. Private members not inherited.
  3. Protected member is inherited as it is.

So, the protected inheritance means all the inherited members as protected. In the private inheritance, in the derived class,

  1. Base class public and protected member are private.
  2. Private members are not inherited.

Even though C++ language supports protected and private inheritance, the most used inheritance is a public inheritance. If developer want to stop further inheritance, they will go for a private inheritance.

Categories: C++

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.