Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance and Polymorphism

Similar presentations


Presentation on theme: "Inheritance and Polymorphism"— Presentation transcript:

1 Inheritance and Polymorphism

2 Syllabus Multipath hybrid inheritance
Dynamic polymorphism – virtual functions Dynamic polymorphism – pure virtual functions 2

3 Problem Given the details about a railway employee, do OOP design and compute the fare to be paid by a railway employee. The railway employee is treated as the privileged passenger. The discount offered vary based on their cadre of employment

4 Loan Application Problem
Input Output Logic Involved Name, age, cadre, date of travel, coach preference, berth preference Name, age, date of travel, coach preference, berth preference Total fare to paid Use hybrid inheritance

5 Inheritance

6 Case Study Railway Reservation System
Privileged Passenger – a passenger who is an employee of the Indian railways system He is given a discount in travel ticket Privileged Passenger is a passenger as well as an employee of railways system Both of them are basically person with basic details like name age etc 6

7 Multi Level Inheritance

8 Multi level Inheritance

9

10 Multiple Inheritance

11 Multipath Inheritance

12

13 COMPILATION ERROR Problem in multipath inheritance
Privileged passenger has datamembers from both regular employee as well as passenger Regular employee and passenger are inherited from person class So duplicate members Compiler gives ERROR ! Also called Diamond Problem

14 Simple Example

15

16

17

18

19

20

21

22

23

24

25

26

27 Virtual Functions

28 Base class vs Derived class Pointers
Base class pointer can hold address of a derived class object but not vice versa Pointer of animal class can hold address of an object of either class lion or tiger and even address of object of liger

29

30 Base class pointer ‘a’ has address of derived class liger but invokes member functions in base class
Due to static binding – Done during compilation Similar to connecting to a call for a landline connection – routing is always same and done during installation But we need dynamic binding Similar to routing a call for a mobile phone – routing is done at the call time based on the location of phone

31

32 Job Scheduling Problem
A number of jobs may be submitted to an Operating System (OS). Assume that the OS follows shortest job first scheduling that is when a number of jobs namely j1, j2, j3 are submitted at the same time, the job which requires minimum time to complete is served first. Similar to case when three customers enter a shop at the same time, customer who is getting least number of items is served first. An OS named as OS1_X supports two types of scheduling. Based on time required to complete job Based on memory required to complete job Design an OOP model and implement the same in C++ to perform the scheduling. Assume that all the jobs are submitted at the same timeGive the solution, based on the choice of scheduling opted by the user.

33 Job Scheduling Problem
Input Output Logic Involved Number of jobs Job name, job id, time required to complete job, memory required to complete job Choice by user for scheduling (time/space) List of names of jobs sorted in ascending order either based on time /memory OOP design using inheritance, Overriding, Sorting

34

35

36

37

38

39

40

41

42

43

44

45


Download ppt "Inheritance and Polymorphism"

Similar presentations


Ads by Google