C++ Inheritance Concept
C++ Inheritance Concept
TYPES OF INHERITANCE
C++ Inheritance
• In C++, inheritance is a process in which one
object acquires all the properties and behaviors
of its parent object automatically. In such way,
you can reuse, extend or modify the attributes
and behaviors which are defined in other class.
• In C++, the class which inherits the members of
another class is called derived class and the class
whose members are inherited is called base class.
The derived class is the specialized class for the
base class.
• To inherit from a class, we use the : symbol.
Advantage of C++ Inheritance
• Code reusability: Now you can reuse the
members of your parent class. So, there is no
need to define the member again. So less
code is required in the program.
Disadvantage of C++ 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.
Types Of Inheritance
C++ supports five types of inheritance:
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance
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:
Why and when to use inheritance?
• 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.
Why and when to use inheritance?
• 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).
Base Class and Derived Class
• Base Class: The class that is being inherited by other class is known as
parent class, super class or base class.
• Derived Class: A class that inherits another class is known as child class, it
is also known as derived class or subclass.
• For example, Here, the Dog class is derived from the Animal class.
Since Dog is derived from Animal, members of Animal are accessible
to Dog.
Types of Inheritance
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.
Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
Types of Inheritance
2. Multilevel Inheritance: In this type of
inheritance, a derived class is created from
another derived class.
How Multilevel Inheritance Works
• The special feature of this type of inheritance
is that the inheritance level can be extended
to any level of inheritance.
• Based on the access modifier or visibility the
scope is caught and the properties of the base
class get inherited. Access modifiers can be
anything from private, public and protected.
Types of Inheritance
3. 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.
Syntax:
class subclass_name : access_mode base_class1,
access_mode base_class2, ....
{
//body of subclass
};
Multiple Inheritance
Types of Inheritance
4. 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.
Types of Inheritance
5. 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:
C++ Inheritance Access Control
WHAT IS INHERITANCE ACCESS CONTROL?
• When creating a derived class from a base class then, you
can use different access specifiers to inherit the data
members of the base class.
• The derived class can access all the non-private members of
its base class. And the base class members that are not
accessible to the member functions of derived classes
should be declared private in the base class.
• The access specifiers that are used are public, private and
protected.
• When deriving a class from a base class, the base class may
be inherited through public, private and protected
inheritance.
A BRIEF DESCRIPTION ABOUT THESE
SPECIFIERS:
• PUBLIC INHERITANCE:
When inheriting a class from a public parent class,
public members of the parent class become public
members of the child class and protected members
of the parent class become protected members of
the child class.
The parent class private members cannot be
accessible directly from a child class but can be
accessible through public and protected members
of the parent class.
A BRIEF DESCRIPTION ABOUT THESE
SPECIFIERS:
• PRIVATE INHERITANCE:
When we derive from a private parent class, then
public and protected members of the parent class
become private members of the child class.
• PROTECTED INHERITANCE:
When deriving from a protected parent class, then
public and protected members of the parent class
become protected members of the child class.