Inheritance

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 Single, Multiple and Multilevel Inheritance with implement
code.

Objectives:
1. Boost software development efficiency by employing inheritance for code reuse and
abstraction, thereby reducing development time.
2. Improve memory efficiency through the utilization of inheritance to construct hierarchical
structures, thereby minimizing redundant data storage and optimizing memory usage.
3. Streamline code execution time by leveraging inheritance to craft efficient class hierarchies,
thereby mitigating method call overhead and enhancing overall program performance.
4. Elevate code performance by harnessing inheritance to foster modular design and
encapsulation, facilitating easier maintenance and scalability.
5. Mitigate code redundancy by leveraging inheritance to establish common parent classes and
inherit shared functionality, resulting in consistent outcomes and reducing storage costs
associated with redundant code.

Introduction: In C++, inheritance is a fundamental object-oriented programming concept that


allows a class (known as the derived class or subclass) to inherit properties and behavior from
another class (known as the base class or superclass). This enables the derived class to reuse
code from the base class, promoting code reuse, modularity, and abstraction. Inheritance
establishes an "is-a" relationship between classes, where the derived class is a specialized version
of the base class. The derived class inherits all the members (attributes and methods) of the base
class, including public and protected members, and can also add its own members or override
inherited members.
There are different types of inheritance in C++:
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Single Inheritance: Single inheritance is a fundamental concept in object-oriented programming
(OOP) where a derived class (or subclass) inherits properties and behavior from only one base
class (or superclass). In single inheritance, a class can have only one direct parent class, forming
a linear hierarchy of classes.

Multiple Inheritance: Multiple inheritance in C++ refers to a feature where a class can inherit
attributes and behaviors from more than one base class. This means a derived class can have
multiple parent classes, each contributing their own set of features to the derived class.

Class A Class B Base

Class C Derived
cass
Multilevel Inheritance: Multilevel inheritance in C++ refers to a situation where a derived class
is created from another derived class. In multilevel inheritance, a class serves as a base class for
another derived class, which in turn can serve as a base class for yet another derived class,
forming a hierarchy of inheritance.
Implementation Code For Single Inheritance:

Output:
Implementation Code For Multiple Inheritance:
Output:

Implementation Code For Multilevel Inheritance:


Output:

Conclusion: During this lab, I extensively explored inheritance within C++. The focus was on
understanding its diverse manifestations, including single, multiple, and hierarchical inheritance,
and mastering their effective implementation. Through the creation of base and derived classes, I
harnessed inheritance's capability to facilitate code reuse and enhance maintainability.
Throughout the exercises, I discerned the advantages of inheritance, notably its role in refining
code organization and fostering adaptability. Challenges such as the "diamond problem" in
scenarios involving multiple inheritance were also encountered. Nevertheless, by attaining
proficiency in inheritance, I acquired a potent instrument for crafting modular and scalable C++
codebases.
In essence, inheritance stands as a cornerstone of object-oriented programming, pivotal in
structuring software systems. This lab experience has endowed me with the expertise to adeptly
employ inheritance in our software development endeavors, amplifying efficiency and efficacy.

You might also like