session11
session11
CODEX
HUB
CODE ACADEMY
Prepared by Elsayed Mustafa
Session 11
Inheritance in C++
15 February, 2024
🟢 Session Topics
What is Inheritance?
Benefits of Inheritance
Constructor Call Order
Function Overriding & Overloading
Multiple & Multilevel Inheritance
What is Inheritance?
1. Inheritance is a powerful feature of object-oriented
programming.
reusability.
and money.
by reducing redundancy.
Example of
Inheritance in C++
Example of Inheritance in C++
1. Animal is the base class with a function makeSound().
public inheritance.
3. Dog can access makeSound() from Animal and also has its
extend functionality.
The "is a" Relationship
1. The relationship between a Base Class
and a derived class is called an "is a"
relationship.
2. Example relationships:
3. A postgraduate student "is a" Student.
4. An Employee "is a" Person.
5. A Salaried Employee "is a" Employee.
6. A car "is a" vehicle.
7. Specialized objects have all
characteristics of the general object
plus additional unique traits.
8. Inheritance in OOP creates the "is a"
relationship.
Inheritance in C++ Code
1. The Person class defines attributes
and methods like set_age() and
get_name().
2. The Student class inherits from
Person but does not need to
redefine inherited methods.
3. This demonstrates code reuse and
hierarchy.
Modes of Inheritance
1. Public Mode:
a. Public members of the base class remain public in the derived class.
b. Protected members remain protected.
2. Protected Mode:
a. Public and protected members of the base class become protected in the
derived class.
3. Private Mode:
a. Public and protected members of the base class become private in the
derived class.
b. Private members cannot be accessed directly in derived classes.
Order of Constructor Call with Inheritance in C++
1. Whether the derived class’s default constructor or