Inheritance and Polymorphism Syllabus Multipath hybrid inheritance Dynamic
Inheritance and Polymorphism
Syllabus • Multipath hybrid inheritance • Dynamic polymorphism – virtual functions • Dynamic polymorphism – pure virtual functions
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
Loan Application Problem Input Output Logic Involved Name, age, cadre, date of travel, coach preference, berth preference Name, age, date of Use hybrid travel, coach inheritance preference, berth preference Total fare to paid
Inheritance 5
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
Multi Level Inheritance
Multi level Inheritance
Multiple Inheritance
Multipath Inheritance
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
Simple Example
Virtual Functions
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
• 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
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 j 1, j 2, j 3 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 OS 1_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 time. Give the solution, based on the choice of scheduling opted by the user.
Job Scheduling Problem Input Output 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 Logic Involved OOP design using inheritance, Overriding, Sorting
34
- Slides: 45