My Blog
Articles related to programming, computer science, technology and research.

27/09/2016 Categories: C++ Programming. Comments Off on Inheritance in C++



One of the main advantages of object oriented programming is reusability. Using already existing code is known as reusability. C++ supports reusability through inheritance which is creating a new class from already existing class.


Inheritance is defined as deriving properties and behavior from one class to another class. The existing class from which properties and behavior are derived is known as a base class or super class. The new class into which the properties and behavior are derived is known as a derived class or sub class.


Inheritance is also known as is-a relationship between classes. For example a Employee is a Person and similarly Programmer is a Person. Common properties and behavior of Employee and Programmer will be available in Person class and specific properties and behavior are present in Employee and Programmer class. Inheritance or is-a relationship between a base class and derived class is represented as follows:




Defining a derived class


Syntax for creating a derived class is as follows:



In the above syntax access-specifier is optional and the valid values for it are private, protected, or public. If access specifier is not provided, by default it is private mode.


Access Specifiers


In C++ there are three access specifiers: private, protected, and public. Let’s look at each one of them in detail.



Private is the most restrictive access specifier. Class members which are declared as private cannot be accessed outside the class (except in friend classes and friend functions). In private derivation, protected and public members become private members in the derived class and cannot be inherited further.



Protected is less restrictive than private. Class members which are declared as protected are accessible only within the class and in the direct child classes of that class. In protected derivation, protected and public members of base class become protected members of derived class.



Public is the least restrictive access specifier. Class members which declared as public are accessible from anywhere. In public derivation, protected and public members of base class become protected and public members of derived class respectively.


Accessibility of the three visibility modes is as shown in the following table:




Following program demonstrates inheritance in C++:



In the above program variables y and z along with the three public functions are accessible in the Derived class. In main class, only z of Base class is accessible directly.


Constructors and Destructors in Inheritance


In inheritance following points should be remembered while working with constructors and destructors:

  • When a base class contains a constructor, derived class must also have a constructor.
  • When a base class constructor contains one or more parameters, the derived class constructor have to pass one or more constructors to the base class.
  • When an object for derived class is created, base class constructors is executed first and then derived class constructor gets executed.
  • The execution of destructors is in reverse order of constructors execution. Derived class destructor executes first and then base class destructor gets executed.


Following example demonstrates constructors and destructors in inheritance:



Invoking Base Class Constructor with Parameters


When a base class constructor contains one or more parameters, it is the responsibility of derived class constructor to pass the required parameters to the base class constructor. Syntax for passing parameters to base class constructor is as follows:



Following program demonstrates invoking base class constructor with parameters from a derived class constructor:



Types of Inheritance


Following figure illustrates different types of inheritance in C++:




Simple Inheritance


In simple inheritance, there is only one super class and one sub class. This is the simplest of all the types of inheritance. Following program demonstrates simple inheritance:



Multi-Level Inheritance


In multi-level inheritance, a sub class inherits a super class and once again that sub class is inherited by another sub class and so on. At each level we have one super class and one sub class. Following program demonstrates multi-level inheritance:



Multiple Inheritance


In multiple inheritance, multiple super classes will be inherited by a single sub class. In multiple inheritance there might be ambiguity problem in sub class.


For example if super class A contains a variable x and another super class B also contains a variable x, then in sub class C, when x is referred, compiler will be confused whether it belongs to class A or class B. To avoid this ambiguity, we use the scope resolution operator.


Following program demonstrates multiple inheritance and the solution to ambiguity problem:



Hierarchical Inheritance


In hierarchical inheritance, a single super class is inherited by multiple sub classes. Following program demonstrates hierarchical inheritance:



Hybrid Inheritance


This is a combination of any two or more of the types of inheritance mentioned before. Following program demonstrates hybrid inheritance:



Multipath Inheritance


Multipath inheritance is a special case of hybrid inheritance. It is a combination of multiple, hierarchical, and multi-level inheritance.


Diamond Problem


In multipath we encounter an ambiguous situation which is also known as diamond problem. For example, let the super class A contains a variable x, which is inherited by sub classes B and C. Now, sub class D of super classes B and C will be able to access the variable x. So, whose x is it? Is it B’s x or C’s x?


To avoid this ambiguous situation and to maintain only one copy of x, we mark the base class A as virtual class. Syntax for making A as virtual base class is as follows:


class B : virtual public A { … }

class C : virtual public A { … }


Following program demonstrates multipath inheritance and virtual base class solution for the diamond problem:



Note: The constructor of a virtual base class is invoked before any non-virtual base class. If there are multiple virtual base classes, then they are invoked in the order in which they are declared.


Object Slicing


In inheritance we can assign a derived class object to a base class object. But, a base class object cannot be assigned to a derived class object. When a derived class object is assigned to a base class object, extra features provided by the derived class will not be available. Such phenomenon is called object slicing.


Following program demonstrates object slicing:



In the above program, when is called, even though class B has variable y, we were not able to access it; which is treated as object slicing.


Pointer to Derived Class


A base class pointer can point to a derived class object. But, a derived class pointer can never point to a base class object. Following program demonstrates object slicing using a base class pointer:



Advantages of Inheritance

  • Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.
  • Reusability enhances reliability. The base class code will be already tested and debugged.
  • As the existing code is reused, it leads to less development and maintenance costs.
  • Inheritance makes the sub classes follow a standard interface.
  • Inheritance helps to reduce code redundancy and supports code extensibility.
  • Inheritance facilitates creation of class libraries.


Disadvantages of Inheritance

  • Inherited functions work slower than normal function as there is indirection.
  • Improper use of inheritance may lead to wrong solutions.
  • Often, data members in the base class are left unused which may lead to memory wastage.
  • Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.


Related Links:

Scroll Up