Inheritance in c++

Inheritance in c++

Inheritance:- It is one of the most powerful features of Object Oriented Programming (OOP) is introduced and explained. The focus is on how a class can be defined and declared as a base class and derived class for implementing the mechanism of single inheritance and multiple inheritance. The syntax and semantic rules of the class hierarchies and levels of inheritance are discussed and illustrated with numerous examples.

Introduction

It is well known that the C++ is a tool for Object Oriented Programming that supports most of the OOP features such as data hiding, data encapsulation, inheritance, polymorphism, virtual functions etc. Data hiding and encapsulation are important features of the object oriented programming and how these concepts are implemented using the classes are explained in previous chapter “Classes and Objects”. The class alone is not enough to support and design OOP. In order to maintain and reuse the class objects easily, it is required to relate disparate classes into another. This chapter presents the salient features of the inheritance that probably is the most powerful features of the object oriented programming.

Inheritance is the process of creating new classes from an existing class. The existing class is known as the base class and the newly created class is called as a derived class. In some OOP languages such as Simula. the base and derived classes are called as super and subclasses respectively. The derived class inherits all capabilities of the base class. It can also add some more features to this class. The base class is unchanged by its process.

The main advantages of the inheritance are:

  1. reusability of the code,
  2. to increase the reliability of the code, and
  3. to add some enhancements of the base class.

Once the base class is written and debugged, it need not be changed again when there are circumstances to add or modify the member of the class.

Single Inheritance:

Single inheritance is the process of creating new classes from an existing base class. The existing class is known as the direct base class and the newly created class is called as a singly derived class.

Single inheritance is the ability of a derived class to inherit the member functions and variables of the existing base class.

Defining the derived class: The declaration of singly derived class is as that same of an ordinary class. A derived class consists of the following components:

  1. the keyword class,
  2. the name of the derived class,
  3. a single colon,
  4. the type of derivation (private, public or protected),
  5. the name of the base or parent class, and
  6. the remainder of the class definition.

Multiple inheritance: It is the process of creating a new class from more than one base classes. the syntax for multiple inheritance is similar to that of single inheritance

Multiple inheritance is a derived class declared to inherit properties of two or more parent classes (base classes). Multiple inheritance can combine the behaviour of multiple base classes in a single derived class. Multiple inheritance has many advantages over the single inheritance such as rich semantics and the ability to directly express complex structures. In C++, derived classes must be declared during the compilation of all possible combinations of derivations and the program can choose the appropriate class at run time and create object for the application.

In a single inheritance, a derived class has a single base class. In multiple inheritance, a derived class has multiple base classes. In a single inheritance hierarchy, a derived class typically represents a specialization of its base class. In a multiple inheritance hierarchy, a derived class typically represents a combination of its base classes. The rules of inheritance and access do not change from a single to a multiple inheritance hierarchy. A whether the inheritance links are private, protected or public.

1 thought on “Inheritance in c++”

Leave a Comment