0% found this document useful (0 votes)
3 views21 pages

Lecture 8-Week8_Multilevel Inheritance, Multiple Inheritance

This document covers concepts of multi-level and multiple inheritance in object-oriented programming. It explains how multiple classes can derive from a single base class and how a derived class can inherit from multiple base classes, highlighting the potential issue of duplicated data members in multiple inheritance scenarios. A solution to this problem, virtual base inheritance, is mentioned as a topic for future discussion.

Uploaded by

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

Lecture 8-Week8_Multilevel Inheritance, Multiple Inheritance

This document covers concepts of multi-level and multiple inheritance in object-oriented programming. It explains how multiple classes can derive from a single base class and how a derived class can inherit from multiple base classes, highlighting the potential issue of duplicated data members in multiple inheritance scenarios. A solution to this problem, virtual base inheritance, is mentioned as a topic for future discussion.

Uploaded by

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

Object Oriented

Programming
(CS1143)
Week 8

Department of Computer Science


Capital University of Science and Technology (CUST)
Outline

 Multi-level Inheritance
 Multiple Inheritance

2
Class Hierarchies

 So far we have only seen one derived class inheriting from one base
class. However all of the following cases are possible.
1. Multiple classes can be derived from the same base class.
2. A base class can also derive from another base class (Multi-level
inheritance)
3. A derived class can derive from multiple base classes (Multiple Inheritance)

3
Multiple Classes Deriving from the
Same Base Class

4
Multi-level Inheritance

5
Multiple Inheritance

6
Outline

 Multi-level Inheritance
 Multiple Inheritance

7
Multiple Inheritance

 We will explain this concept using an example.

8
9
10
Description

 The program creates an instance of Faculty.


 Since Faculty is derived from Employee and Employee is derived from
Person , Faculty ’s constructor invokes Employee ’s constructor before
it performs its own task.
 Employee ’s constructor invokes Person ’s constructor before it
performs its own task,

11
Description Continued

 When the program exits, the Faculty object is destroyed. So the


Faculty ’s destructor is called, then Employee ’s, and finally Person ’s

12
Program Discussion (W8-P2-
S23.cpp)

13
Outline

 Multi-level Inheritance
 Multiple Inheritance

14
Multiple Inheritance

 C++ allows multiple inheritance, the derivation of a class from more


than one class.
 As a simple example, we can have a class named TA (teaching
assistant) that is inherited from two classes: Student and Professor

15
Program Discussion (W8-P3-
S23.cpp)

16
Multiple Inheritance-Diamond
Problem
 Suppose we have a class Person that defines one single data member,
name.
 Both Student and Professor classes inherit from Person class.
 This data member is inherited in both the Student object and the
Professor object.
 Since the TA (teaching assistant) class inherits the Student and
Professor classes, the data member name is duplicated in the TA
class.
 We cannot use inheritance in this case without removing the
duplicate data member.

17
18
Program Discussion (W8-P4-
S23.cpp)

19
Solution

 One solution for the problem of duplicated shared data members in


multiple inheritance is to use virtual base inheritance.
 We will study it in Week 11 inshallah.

20
This is all for Week 8

21

You might also like