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

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Information Security of Embedded Systems : Design of Secure Systems Prof. Dr. Holger Schlingloff Institut für Informatik und Fraunhofer FIRST.
What is the End-Goal? Interoperability? Integration? Connectivity?
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
A useful testing technique and more…
Hash Tables and Constant Access Time CS-2303, C-Term Hash Tables and Constant Access Time CS-2303 System Programming Concepts (Slides include materials.
Tutorial 1 Creating a Database
® Microsoft Office 2010 Integrating Word, Excel and Access.
® Microsoft Office 2010 Integrating Word, Excel, Access, and PowerPoint.
Collaboration Works! 10/20/20101 Planning Research Institutional Effectiveness.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
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.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
Programming Assignment #4 Binary Trees
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Inheritance.
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.
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.
C++ facts From What all is inherited from parent class in C++? Following are the things which a derived class inherits from its.
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
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Object-Oriented Programming Chapter Chapter
ISBN Object-Oriented Programming Chapter Chapter
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Inheritance Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
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
Lecture 22 Inheritance Richard Gesick.
Advanced Java Topics Chapter 9
Chapter 20- Virtual Functions and Polymorphism
“Under the Hood” of Polymorphism
Polymorphism Polymorphism - Greek for “many forms”
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
The Assembly Language Level
Chapter 20 - C++ Virtual Functions and Polymorphism
C++ Object Oriented 1.
Introduction to Classes and Objects
Presentation transcript:

Under the Hood of Polymorphism CS-2303, C-Term Under the Hood of Polymorphism CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Under the Hood of Polymorphism CS-2303, C-Term Review – Virtual Functions Define a method as virtual, and the subclass method overrides the base class method E.g., class Shape { public: virtual void Rotate(); virtual void Draw();... } Compiler to adds internal pointers to every object of class Shape and its derived classes, so that pointers to correct methods can be accessed from any object via any pointer. Creates three levels of indirection to access a virtual method.

Under the Hood of Polymorphism CS-2303, C-Term Virtual Function Table (vtable) created for any class with virtual function(s) One for base class, one for each derived class Vtable entries point to methods for appropriate class Constructor sets a pointer in each object to point to appropriate vtable Executing program indexes into vtable to select appropriate function implementation for each function call © by Pearson Education, Inc. All Rights Reserved. Compiling Virtual Functions

Under the Hood of Polymorphism CS-2303, C-Term © by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig Pointer to object (level 1)

Under the Hood of Polymorphism CS-2303, C-Term © by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig Pointer to vtable (stored in every object – level 2)

Under the Hood of Polymorphism CS-2303, C-Term © by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig Index into vtable to access method (level 3)

Under the Hood of Polymorphism CS-2303, C-Term Performance Impact Nominal Must access two additional pointers to get to method Versus direct access for non- virtual methods

Under the Hood of Polymorphism CS-2303, C-Term 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

Under the Hood of Polymorphism CS-2303, C-Term 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). © by Pearson Education, Inc. All Rights Reserved.

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

Under the Hood of Polymorphism CS-2303, C-Term Questions?

Under the Hood of Polymorphism CS-2303, C-Term