“Under the Hood” of Polymorphism

Slides:



Advertisements
Similar presentations
Under the Hood of Polymorphism CS-2303, C-Term Under the Hood of Polymorphism CS-2303 System Programming Concepts (Slides include materials from.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
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.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
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.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 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.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Big O And your first reading assignment
Object-Oriented Programming
CS212: Object Oriented Analysis and Design
Comp 249 Programming Methodology
Programming Assignment #4 Binary Trees in C++
Advanced Java Topics Chapter 9
Makefiles and Notes on Programming Assignment PA2
Chapter 20- Virtual Functions and Polymorphism
Polymorphism Polymorphism
Classes, Constructors, etc., in C++
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Miscellaneous C++ Topics
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Structures, Unions, and Typedefs
Polymorphism CMSC 202, Version 4/02.
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Bit Fields & Bitwise Operations
Programming Assignment #1 12-Month Calendar—
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Recursion and Implementation of Functions
Programming Assignment #6
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Scope Rules and Storage Types
Programming Assignment #5
Chapter 20 - C++ Virtual Functions and Polymorphism
Your first C and C++ programs
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Digression on Loop Invariants
A Deeper Look at Classes
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.
Introduction to Classes and Objects
Presentation transcript:

“Under the Hood” of Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Under the Hood

Review – Virtual Functions E.g., class Shape { public: virtual ~Shape(); virtual void Rotate(); virtual void Draw() = 0; ... } virtual means that derived class may override method. pure virtual means that derived class must override method. CS-2303, A-Term 2012 Under the Hood

Implementation Overview Compiler adds table of function pointers Every object of class with virtual Including every derived class Result:– possible to reach correct method from any object See Absolute C++, §15.2 (end) CS-2303, A-Term 2012 Under the Hood

Compiling Virtual Functions Virtual Function Table (vtable) Internal static “member” added to base class And to each derived class Contains function pointers for all virtual functions See Absolute C++, §15.2 (end) CS-2303, A-Term 2012 Under the Hood

Compiling Virtual Functions (continued) Constructor sets a pointer in each object to point to appropriate vtable Executing program indexes into vtable to select appropriate method ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Example Abstract base class ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Pointer to object (level 1) ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Pointer to vtable (stored in every object – level 2) ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Index into vtable to access method (level 3) ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Performance Impact “Nominal” Must access two additional pointers to get to method Versus direct access for non-virtual methods ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Trade-off Nominal performance impact vs. clarity of thought and ease of programming Most of the time, clarity of thought wins Especially with modern optimizing compilers In some (rare) cases, performance is essential Note:– Java has similar implementation “under the hood” for all methods ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Software Engineering Observation Dynamic binding enables independent software vendors (ISVs) to distribute software without revealing proprietary secrets. Software distributions can consist of only header files and object files No source code needs to be revealed. Software developers use inheritance to derive new classes from those provided by the ISVs Previous software that works with the ISVs classes still work with the derived classes Will use the overridden virtual functions provided in these classes (via dynamic binding). ©1992-2010 by Pearson Education, Inc. All Rights Reserved. CS-2303, A-Term 2012 Under the Hood

Software Engineering Observation (example) Develop a plug-in for, say, Photoshop Can still use all of Photoshop’s tools on your images Powerful method for innovation, leveraging existing code to drive new ideas CS-2303, A-Term 2012 Under the Hood

Questions? CS-2303, A-Term 2012 Under the Hood