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

Experiment No: 04

Experiment Name: Introduction to Inheritance and its classification with Implement Code.

Objectives:
1. Improve software development efficiency by leveraging inheritance to reduce development
time through code reuse and abstraction.

2. Enhance memory efficiency by utilizing inheritance to create hierarchical structures,


minimizing redundant data storage and optimizing memory usage.

3. Optimize code execution time by employing inheritance to create efficient class


hierarchies, reducing method call overhead and improving overall program performance.

4. Enhance code performance by utilizing inheritance to promote modular design and


encapsulation, enabling easier maintenance and scalability.

5. Reduce redundancy in code by utilizing inheritance to establish common parent classes and
inherit shared functionality, leading to consistent results and minimizing storage costs
associated with redundant code.

Introduction: 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.
Based on Implementation C++ supports five types of Inheritance:
1. Single Inheritance.
2. Multiple Inheritance.
3. Multilevel Inheritance.
4. Hierarchical Inheritance.
5. Hybrid Inheritance.

But here I will, discuss about three types of Inheritance.

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.

There exists single base class and single derived class.


Multiple Inheritance: In Multiple inheritances, a derived class inherits from multiple base
classes. It has properties of both the base classes.

Class A Class B Base

Class C Derived
cass

Multilevel Inheritance: In Multilevel inheritance, a class inherits its properties from another
derived class.

Class A it is a Base class (super) of B

it is a sub class (derived) of A


Class B
and base class of class C

Class C derived class(sub) of class B


Implement Code:

Single Inheritance:

Output:
Multiple Inheritance:

Output:
Multilevel Inheritance:
Output:

Conclusion:

In this lab, I delved into the concept of inheritance in C++. We explored its different forms,
such as single, multiple, and hierarchical inheritance, and learned how to implement them
effectively. By creating base and derived classes, we harnessed inheritance's power to
promote code reuse and maintainability.

Throughout the exercises, I identified the benefits of inheritance, including improved code
organization and flexibility. I also encountered challenges like the "diamond problem" in
multiple inheritance scenarios. Nonetheless, by mastering inheritance, I gained a valuable
tool for designing modular and scalable C++ code.

In summary, inheritance is a cornerstone of object-oriented programming, and our


exploration in this lab has equipped us with the skills to leverage it efficiently in our software
development endeavors.

You might also like