Inheritance in C++: Why and When To Use Inheritance? Modes of Inheritance Types of Inheritance

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Inheritance in C++

The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important feature of Object Oriented
Programming.
Sub Class: The class that inherits properties from another class is called Sub class or
Derived Class.
Super Class:The class whose properties are inherited by sub class is called Base
Class or Super class.
The article is divided into following subtopics:
1. Why and when to use inheritance?
2. Modes of Inheritance
3. Types of Inheritance
Why and when to use inheritance?
Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The
methods fuelAmount(), capacity(), applyBrakes() will be same for all of the three
classes. If we create these classes avoiding inheritance then we have to write all of
these functions in each of the three classes as shown in below figure:

You can clearly see that above process results in duplication of same code 3 times.
This increases the chances of error and data redundancy. To avoid this type of
situation, inheritance is used. If we create a class Vehicle and write these three
functions in it and inherit the rest of the classes from the vehicle class, then we can
simply avoid the duplication of data and increase re-usability. Look at the below diagram
in which the three classes are inherited from vehicle class:
Using inheritance, we have to write the functions only one time instead of three times as
we have inherited rest of the three classes from base class(Vehicle).
Implementing inheritance in C++: For creating a sub-class which is inherited from the
base class we have to follow the below syntax.
Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
Here, subclass_name is the name of the sub class, access_mode is the mode in
which you want to inherit this sub class for example: public, private etc.
and base_class_name is the name of the base class from which you want to inherit the
sub class.
Note: A derived class doesn’t inherit access to private data members. However, it does
inherit a full parent object, which contains any private members which that class
declares.
In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the
public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.
Modes of Inheritance
1. Public mode: If we derive a sub class from a public base class. Then the public
member of the base class will become public in the derived class and protected
members of the base class will become protected in derived class.
2. Protected mode: If we derive a sub class from a Protected base class. Then both
public member and protected members of the base class will become protected in
derived class.
3. Private mode: If we derive a sub class from a Private base class. Then both public
member and protected members of the base class will become Private in derived
class.
Note : The private members in the base class cannot be directly accessed in the
derived class, while protected members can be directly accessed. For example,
Classes B, C and D all contain the variables x, y and z in below example. It is just
question of access.
filter_none
The below table summarizes the above three modes and shows the access specifier of
the members of base class in the sub class when derived in public, protected and
private modes:

Types of Inheritance in C++


1. Single Inheritance: In single inheritance, a class is allowed to inherit from only
one class. i.e. one sub class is inherited by one base class only.

Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from
more than one classes. i.e one sub class is inherited from more than one base classes.

Multilevel Inheritance: In this type of inheritance, a derived class is created from another

derived class. Hierarchical Inheritance: In


this type of inheritance, more than one sub class is inherited from a single base class. i.e. more
than one derived class is created from a single base class.

Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one
type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance:
Polymorphism in C++
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
Real life example of polymorphism, a person at a same time can have different
characteristic. Like a man at a same time is a father, a husband, a employee. So a
same person posses have different behavior in different situations. This is called
polymorphism.
Polymorphism is considered as one of the important features of Object Oriented
Programming.
In C++ polymorphism is mainly divided into two types:
 Compile time Polymorphism
 Runtime Polymorphism
1. Compile time polymorphism: This type of polymorphism is achieved by function
overloading or operator overloading.

 Function Overloading: When there are multiple functions with same name
but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of
arguments or/and change in type of arguments.
Operator Overloading: C++ also provide option to overload operators. For example, we can
make the operator (‘+’) for string class to concatenate two strings. We know that this is the
addition operator whose task is to add to operands. So a single operator ‘+’ when placed
between integer operands , adds them and when placed between string operands,
concatenates them.

Runtime polymorphism: This type of polymorphism is achieved by Function


Overriding.
 Function overriding on the other hand occurs when a derived class has a
definition for one of the member functions of the base class. That base function is
said to be overridden.

You might also like