Dynamic Binding Implementation Object-Oriented Programming 236703 Spring 2008 1.

Slides:



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

Object Oriented Programming with Java
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Chapter 12: Support for Object-Oriented Programming
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Polymorphism, Virtual Methods and Abstract Classes.
The Procedure Abstraction Part IV: Run-time Structures for OOLs Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
1 Challenge. 2 Converter.java package prg; public class Converter { private int value; public Converter(int v) { value = v; } public int dollarToShekel()
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Dynamic Binding Object-Oriented Programming Spring
Chapter 12 Support for Object-Oriented Programming.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
Variations on Inheritance Object-Oriented Programming Spring
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
A First Book of C++ Chapter 12 Extending Your Classes.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Polymorphism, Virtual Methods and Abstract Classes
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Semantic Analysis with Emphasis on Name Analysis
Class A { public : Int x; A()
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
PZ09A - Activation records
Support for Object-Oriented Programming
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Polymorphism.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Inheritance, Polymorphism and the Object Memory Model
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Computer Science II for Majors
Presentation transcript:

Dynamic Binding Implementation Object-Oriented Programming Spring

Implementation of Virtual Functions Ellipse draw + hide + rotate + Circle rotate ++ centre + class Ellipse { //... public: virtual void draw() const; virtual void hide() const; virtual void rotate(int); } E1, E2, *P; class Circle: public Ellipse { //... public: virtual void rotate(int); virtual Point centre(); } C1, C2, C3; E1E1 E2E2 P C1C1 C2C2 C3C3

Circle :: rotate Ellipse :: rotate Circle :: centre Ellipse :: draw Ellipse :: hide draw hide rotate Ellipse VMT draw hide rotate centre Circle VMT C1C3C2E1E2 P The Virtual Methods Table C++ Jargon: vptr and vtbl

P->rotate() Circle :: rotate Ellipse :: rotate Circle :: centre Ellipse :: draw Ellipse :: hide draw hide rotate Ellipse VMT draw hide rotate centre Circle VMT C1C3C2E1E2 P

Location of VPTR #1/2 Borland Style: at the beginning of an object. – Intuitive and simple (usually) – Problematic, if the base class does not have any virtual functions: – When converting a pointer to the derived class into a pointer to the base, the compiler must add an offset to skip over the vptr. This offset must be subtracted in downcasting. – The compiler must also do a null check, because the offset should not be added in case of a null pointer.

Location of VPTR #2/2 Gnu Style: when first virtual function is encountered Not so simple or intuitive. Virtual function call is a bit more complicated. Compiler must have a deterministic algorithm for locating the vptr: – If the function called is not virtual in the static type of the pointer - use static binding – If the function is virtual - add to the pointer the offset corresponding to the size of the most derived “virtual-free” super-class of the static type of the pointer Casting is so much simpler. No need to add or subtract any offset in up or down casting.

Dynamic Binding and Dynamic Typing Dynamic Typing: no constraints on the values stored in a variable. – Usually implies reference semantics Run-time type information: dynamic type is associated with the value. – There is no notion of static type to be associated with a variable. No type safety: run-time error if an object doesn't recognize a message.

Dispatch Tables Used in dynamic type systems Support: – Runtime introduction of new types – Runtime changes to type hierarchy – “Method not found” error messages uSpace Efficiency: optimal! uTime Efficiency: lousy; mitigated by a cache of triples: lClass where search started lSelector searched lAddress of method found

Binding within Constructors How is an object of class B derived from class A initialized? In C++ and Java, the constructor of A is invoked before the constructor of B – Why? So the B constructor never sees uninitialized attributes What happens if A’s constructor invokes a virtual function?

Binding within Constructors – C++ The binding of function calls within constructors is static – B’s memory has not been initialized yet – The output of new B(); is: x=1 struct A { int x; virtual void f() {cout << “x=“ << x ;} A() : x(1) {f();} }; struct B: A { public: int y; virtual void f() {cout << “y=” << y;} B() : y(2){}; }; struct A { int x; virtual void f() {cout << “x=“ << x ;} A() : x(1) {f();} }; struct B: A { public: int y; virtual void f() {cout << “y=” << y;} B() : y(2){}; };

Problem with Static Binding within Constructors What happens in new B(); ? Some compilers do not allow calling a pure virtual function directly from constructors However, nesting such a call in a chain of function calls it will usually compile struct A { virtual void f() = 0; A() {f();} }; struct B: A { public: virtual void f() {cout << “B’s f”;} }; struct A { virtual void f() = 0; A() {f();} }; struct B: A { public: virtual void f() {cout << “B’s f”;} };

Binding within Constructors – Java The binding of function calls within constructors is dynamic – An initialization phase precedes the constructor invocation – The output of new B(); is: y=0 class A { private int x=1; public void f() {System.out.print(“x=“+x);} public A() {f();} } class B extends A { private int y=2; public void f() {System.out.print(“y=”+y);} public B() {} } class A { private int x=1; public void f() {System.out.print(“x=“+x);} public A() {f();} } class B extends A { private int y=2; public void f() {System.out.print(“y=”+y);} public B() {} }

Problem with Dynamic Binding within Constructors What happens in new B(); ? – s is initialized to null when A ’s constructor is invoked – B ’s toString() is invoked from A ’s constructor – The result: NullPointerException class A { public A() {System.out.print(toString());} } class B extends A { private String s = “Class B” public String toString() {return s.toLowerCase();} } class A { public A() {System.out.print(toString());} } class B extends A { private String s = “Class B” public String toString() {return s.toLowerCase();} }