450 likes | 494 Views
Inheritance and Polymorphism. Syllabus. Multipath hybrid inheritance Dynamic polymorphism – virtual functions Dynamic polymorphism – pure virtual functions. 2. Problem.
E N D
Syllabus Multipath hybrid inheritance Dynamic polymorphism – virtual functions Dynamic polymorphism – pure virtual functions 2
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
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
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
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 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.