Introduction To Inheritance in C
Introduction To Inheritance in C
Inheritance in C++
Inheritance is a fundamental concept in C++ where a new class, called
the derived class, is created based on an existing class, called the base
class. This allows the derived class to inherit the properties and
behaviors of the base class, promoting code reuse and creating
hierarchical relationships between classes.
What is Inheritance?
1 Code Reuse 2 Hierarchical Relationships
Inheritance allows the derived Inheritance creates a hierarchical
class to inherit the members (data relationship between classes,
and functions) of the base class, where the derived class is a more
reducing the amount of code that specialized version of the base
needs to be written. class.
3 Polymorphism
Inheritance enables polymorphism, where objects of the derived class can be
treated as objects of the base class, allowing for dynamic dispatch and flexibility in
code.
Types of Inheritance
Single Inheritance Multiple Inheritance
Hierarchical Inheritance
Multiple derived classes inherit from
a single base class.
Single Inheritance
1 Base/Parent/Super Class
The existing class from which the new
class inherits.
2 Derived/Child/Sub Class
The new class that inherits
from the base class.
3 Inheritance
The process of creating the derived class based on the base class.
Multiple Inheritance
Derived Class Base Classes
The new class that inherits The existing classes from
from multiple base classes. which the derived
class inherits.
Ambiguity Complexity
Derived Classes
The new classes that inherit
from the base class, creating a hierarchy.
Reusability
Hierarchical inheritance promotes
code reuse by allowing multiple derived classes to share the base class's properties and behaviors.
Multi-Level Inheritance
Base Class
The existing class from which
multiple derived classes inherit.
Derived Classes
The new classes that inherit
from the base class, creating a hierarchy.
Reusability
Hierarchical inheritance promotes
code reuse by allowing multiple derived classes to share the base class's properties and behaviors.
Single Inheritance
1 Base Class
The initial class from
which another class is derived.
2 Intermediate Class
A class that is derived from the base class
and acts as a base class for another
derived class.
3 Derived Class
The final class in the inheritance
chain, which is derived from
the intermediate class.
4 Reusability
Multilevel inheritance promotes code reuse by allowing each level of the hierarchy to inherit properties and
behaviors from the level above. Each derived class can further specialize or extend the functionalities provided
by its base classes.
Advantages of Inheritance
3 Polymorphism 4 Modularity
Inheritance enables polymorphism, which allows Inheritance promotes modular design, as the base and
objects of derived classes to be treated as objects of derived classes can be developed and tested
the base class. independently.
Limitations of Inheritance