0% found this document useful (0 votes)
17 views

Introduction To Inheritance in C

Uploaded by

f23ari18
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Introduction To Inheritance in C

Uploaded by

f23ari18
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to

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

A derived class inherits from a single A derived class inherits from


base class. multiple base classes.

Multi-level Inheritance Hybrid Inheritance


A class is derived from another A combination of two or
derived class more types of 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

Multiple inheritance can Multiple inheritance can


introduce ambiguity, which increase the complexity
must be resolved by the of the code, making it more
programmer. difficult to manage and
maintain.
Hierarchical 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.
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

1 Code Reuse 2 Maintainability


Inheritance allows code to be reused across multiple Changes made to the base class can be automatically
classes, reducing development time and effort. reflected in the derived classes, improving
maintainability.

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

Complexity Tight Coupling Ambiguity Performance Impact


Inheritance can increase the Inheritance creates a tight Multiple inheritance can Inheritance can have a
complexity of the coupling between the base introduce ambiguity, which negative impact on
codebase, making it harder and derived classes, which must be carefully managed performance, especially
to understand and maintain. can make the system more to avoid unexpected when dealing with large
rigid. behavior. inheritance hierarchies.

You might also like