Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Polymorphism CS351 – Programming Paradigms.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
In a not gate, if the input is on(1) the output is off (0) and vice versa.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
Chapter 12 Support for Object oriented Programming.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Object-Oriented Programming Chapter Chapter
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
ISBN Chapter 12 Support for Object-Oriented Programming.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism, Virtual Methods and Abstract Classes
Inheritance Allows extension and reuse of existing code
Andy Wang Object Oriented Programming in C++ COP 3330
Quiz # 02 Design a data type Date to hold date
Air New Zealand limited is a flight carrier airline or group of New Zealand airline, based in Auckland. The air new Zealand operates scheduled passenger.
Inheritance and Polymorphism
Air NewZealand Ticket reservations
Function Overloading.
COMP 220 HELP Marvelous Learning / comp220help.com
OOP What is problem? Solution? OOP
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virgin America Airlines Reservations Number
1-888(202)5328 Turkish Airlines Booking Number
Air France Reservations Phone Number
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Designing for Inheritance
Lab 02 - SNAP.
User Guide Portman Concur
Support for Object-Oriented Programming in Ada 95
Inheritance, Polymorphism, and Virtual Functions
Operating Systems.
Dr. Bhargavi Dept of CS CHRIST
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Support for Object-Oriented Programming
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
Polymorphism 2 CMSC 202.
CMSC 202 Lesson 19 Polymorphism 2.
Ninth step for Learning C++ Programming
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Inheritance and Polymorphism
Welcome To Allegiant Air. Allegiant Air Reservations Process Every passenger wants to travel but they restrict themselves due to high fare charges. And.
Presentation transcript:

Inheritance and Polymorphism

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

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

Inheritance

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

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 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.

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