[go: up one dir, main page]

0% found this document useful (0 votes)
4 views17 pages

session11

The document provides an overview of inheritance in C++, explaining its definition, benefits, and various types such as multilevel and multiple inheritance. It discusses the 'is a' relationship in object-oriented programming, constructor call order, and concepts like function overriding and overloading. Examples illustrate how derived classes inherit properties from base classes, enhancing code reusability and reliability.

Uploaded by

amoselgpt241
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

session11

The document provides an overview of inheritance in C++, explaining its definition, benefits, and various types such as multilevel and multiple inheritance. It discusses the 'is a' relationship in object-oriented programming, constructor call order, and concepts like function overriding and overloading. Examples illustrate how derived classes inherit properties from base classes, enhancing code reusability and reliability.

Uploaded by

amoselgpt241
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CODEX

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.

2. It is the process of creating new classes, called derived

classes, from existing or base classes.

3. The derived class inherits all capabilities of the base class

but can add its own features.


Benefits of Inheritance
1. Inheritance enables code

reusability.

2. Reusing existing code saves time

and money.

3. It enhances a program’s reliability

by reducing redundancy.
Example of
Inheritance in C++
Example of Inheritance in C++
1. Animal is the base class with a function makeSound().

2. Dog is the derived class that inherits from Animal using

public inheritance.

3. Dog can access makeSound() from Animal and also has its

own function bark().

4. In main(), an object of Dog calls both makeSound()

(inherited) and bark() (its own function).


Real-World Example of Inheritance
1. The Person class is the base class, with
attributes like name, address, and phone.
2. Employee and Customer are derived

classes with additional attributes.

3. The derived classes inherit the


properties of the Person class but

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

parameterized constructor is called, the base class’s default

constructor is always executed first.

2. To call the base class’s parameterized constructor inside the

derived class, it must be explicitly mentioned.


Function Overriding
1. Function
overriding allows
redefining a base
class function in
the derived class
while maintaining
the same function
signature.
Function Overloading
1. Function overloading allows defining multiple functions with
the same name but different parameters.
2. Can be done in both base and derived classes.
Multilevel Inheritance in C++
1. Multilevel inheritance
occurs when a class is
derived from another
derived class, forming a
chain of inheritance.
2. This allows properties to
be inherited in multiple
stages.
Multiple Inheritance in C++
1. Multiple inheritance
occurs when a class
inherits from more than
one base class.
2. Allows the derived class to
acquire properties from
multiple parent classes.
Thank you

You might also like