CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.

Slides:



Advertisements
Similar presentations
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
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.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
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.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
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.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
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.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Object-Oriented Programming Chapter Chapter
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Chapter -6 Polymorphism
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
Computer Science Department Inheritance & Polymorphism.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
Class A { public : Int x; A()
Object-Oriented Programming
Polymorphism.
Inheritance and Run time Polymorphism
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism Polymorphism
Pointers Dr. Bhargavi Goswami Department of Computer Science
CISC/CMPE320 - Prof. McLeod
Chapter 11: Inheritance and Composition
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
VIRTUAL FUNCTIONS RITIKA SHARMA.
Chapter 11 Class Inheritance
Lecture 6: Polymorphism
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
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism

Recap of lecture 15 Inheritance in C++ Different types of inheritance Private, Protected Virtual

Outline of Lecture 16 Polymorphism Pointer to derived type Virtual function VTABLE, VPTR Inheritance and VTABLE

Polymorphism Polymorphism in C++ Compile Time Function overloading Operator overloading Runtime Virtual Function The provision of a single interface to entities of different types. Methods with same name but different implementations. Operators have different implementations depending on their arguments A member function that you expect to be redefined in derived classes.

Introduction Encapsulation Access control Inheritance Virtual function Creates new data types Separates the interface from the implementation An object as its own type or its base type

Paradigm shift in programming Better C Enhanced version of C Structure  Class Variable declaration Object Based Grouping a data structure together (Encapsulation) Constructor and destructors Inheritance Object oriented Virtual function Intimately bound with the concept of type Understood only from a design viewpoint

Pointers to Derived Types A pointer of one type cannot point to an object of a different type An important exception to this rule that relates only to derived classes B D A base class pointer can point to any derived classes object Demonstration

Virtual Function A virtual function is a member function Declared within a base class Redefined by a derived class “one interface, multiple methods”

Binding Connecting a function call to a function body is called binding. Types of Bindings EarlyLate Before the program is run (by the compiler and linker) Binding occurs at runtime, based on the type of the object Dynamic binding or Runtime binding

Virtual functions To cause late binding to occur for a particular function Use the virtual keyword when declaring the function When a virtual function is redefined, all aspects of its prototype must be the same. Constructor functions cannot be virtual, but destructor functions can. Overriding : virtual function redefinition by a derived class. Demonstration

How C++ implements late binding Compiler takes care of the virtual function mechanism Run-time polymorphism is achieved when access is through a base-class pointer (or reference) Compiler creates a single table (called the VTABLE) for each class Compiler places the addresses of the virtual functions Pointer to the VTABLE for an object: VPTR (vpointer)

Storing type information No explicit type information stored in any of the classes. How the type is determined at run time? The type information is hidden. Demonstration

An Example Instrument; play(), what(), adjust() WindPercussionStringed BrassWoodwind Demonstration

Visualizing virtual functions

Begins with the Instrument pointer Compiler can pick the VPTR of that object VPTR points to the starting address of the VTABLE Compiler knows adjust( ) function is at the location VPTR+2 “Call the function at VPTR+2”

The Virtual Attribute Is Inherited When a virtual function is inherited, its virtual nature is also inherited. Base class Virtual function Derived class 1 Virtual function Derived class 2 Virtual function No matter how many times a virtual function is inherited, it remains virtual

Inheritance and the VTABLE Compiler creates a new VTABLE for your new class For non-overridden virtual functions, inserts the addresses using the base-class function addresses What happens when you inherit and add new virtual functions in the derived class? (Demonstration) RTTI: Run Time Type Identification

Hierarchical Virtual Functions Base class Virtual function Derived class 1 Virtual function Derived class 2 Virtual function Case 1 Base class Virtual function Derived class 1 Virtual function Derived class 2 Case 2 Base class Virtual function Derived class 1 Virtual function Derived class 2 Case 3

Overheads of virtual functions

Thank you Next Lecture: Virtual Function - II