Object Oriented Programming. Spring 2008

Similar documents
Object Model. Object Oriented Programming Winter

Object typing and subtypes

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Case Study: Meta Classes

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Polymorphism Part 1 1

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

VIRTUAL FUNCTIONS Chapter 10

Concepts of Programming Languages

Object Model. Object Oriented Programming Spring 2015

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

What about Object-Oriented Languages?

Object Oriented Programming. Solved MCQs - Part 2

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Dynamic Binding C++ Douglas C. Schmidt

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Object Oriented Software Design II

Object Oriented Software Design II

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

C++ Programming: Polymorphism

CSE 401/M501 Compilers

Binghamton University. CS-140 Fall Dynamic Types

Short Notes of CS201

Java Object Oriented Design. CSC207 Fall 2014

COMP322 - Introduction to C++

CS105 C++ Lecture 7. More on Classes, Inheritance

CS201 - Introduction to Programming Glossary By

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Class, Variable, Constructor, Object, Method Questions

Java Review: Objects

by Pearson Education, Inc. All Rights Reserved.

1 Shyam sir JAVA Notes

Exercise: Singleton 1

04-24/26 Discussion Notes

CH. 2 OBJECT-ORIENTED PROGRAMMING

What is Inheritance?

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Programming Languages and Techniques (CIS120)

CSE 431S Type Checking. Washington University Spring 2013

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Making New instances of Classes

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

Chapter 6 Introduction to Defining Classes

Chapter 10 Object-Oriented Programming

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Programming Languages 2nd edition Tucker and Noonan"

Instantiation of Template class

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Implements vs. Extends When Defining a Class

Example: Count of Points

Information System Design (IT60105)

Absolute C++ Walter Savitch

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

CS-202 Introduction to Object Oriented Programming

Data Abstraction. Hwansoo Han

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Compaq Interview Questions And Answers

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Polymorphism. Zimmer CSCI 330

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Selected Java Topics

INHERITANCE. Spring 2019

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Practice Questions for Chapter 9

Last Class: Multiple Inheritance. Implementing Polymorphism. Criteria. Problem. Smalltalk Message Passing. Smalltalk

Inheritance, Polymorphism and the Object Memory Model

Java: introduction to object-oriented features

More On inheritance. What you can do in subclass regarding methods:

15CS45 : OBJECT ORIENTED CONCEPTS

Introduction to Programming Using Java (98-388)

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Programming, numerics and optimization

Extending Classes (contd.) (Chapter 15) Questions:

Francesco Nidito. Programmazione Avanzata AA 2007/08

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Francesco Nidito. Programmazione Avanzata AA 2007/08

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Supporting Class / C++ Lecture Notes

CS260 Intro to Java & Android 03.Java Language Basics

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Java Primer 1: Types, Classes and Operators

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

C++ Inheritance and Encapsulation

25. Generic Programming

Derived and abstract data types. TDT4205 Lecture 15

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Inheritance Motivation

CSE 307: Principles of Programming Languages

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Object Oriented Paradigm

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Transcription:

Dynamic Binding Implementation Object Oriented Programming 236703 Spring 2008 1

Implementation of Virtual Functions class Ellipse { //... public: E 1 virtual void draw() const; draw E + virtual void hide() const; 2 hide virtual void rotate(int); + P } E1, E2, *P; rotate + Ellipse class Circle: public Ellipse { //... public: virtual void rotate(int); virtual Point centre(); } C1, C2, C3; C 1 C 2 C 3 Circle rotate ++ centre +

The Virtual Methods Table P C++ Jargon: vptr and vtbl E1 E2 C1 C2 C3 Ellipse VMT draw hide rotate Circle VMT draw hide rotate centre Ellipse :: draw Ellipse :: hide Circle :: centre Ellipse :: rotate Circle :: rotate

P->rotate() P E1 E2 C1 C2 C3 Ellipse VMT draw hide rotate Circle VMT draw hide rotate centre Ellipse :: draw Ellipse :: hide Circle :: centre Ellipse :: rotate Circle :: rotate

Location of VPTR #1/2 BorlandStyle: at the beginning of an object. Intuitive and simple (usually) Problematic, if the base class does not have any virtual functions: p Base vtbl Base Derived Derived vptr base object additional data members 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. Thisoffset mustbe 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 virtualfunction isencountered p vtbl Base Base Deriveded Derived base object vptr additional data members 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.

Oliver Twist instance_of other instance variables... Novel subclass_of dispatch_table other class variables... #set_author #get_author th Dispatch Tables Document subclass_of dispatch_table... other class variables -------------- ------ ---- ---- ----- -- ----- ------- ------------- ------ ------------- #set_year #get_year... ------------ -------- --------- ---- ------------- ------------- ---- ------- Object subclass_of dispatch_table other class variables #print... ------- --------- ---- ------------- Used in dynamic type systems Support: Runtimeintroduction introduction of new types Runtime changes to type hierarchy Method not found error messages Space Efficiency: optimal! Time Efficiency: lousy; mitigated by a cache of triples: Class where search started Selector searched Address 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 pp virtual function?

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

Problem with Static Binding within struct A { virtual void f() = 0; A() {f();} }; Constructors t struct B: A { public: virtual void f() {cout << B s f ;} }; 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

Binding within Constructors Java The binding of function calls within constructors is dynamic An initialization iti phase precedes the constructor t invocation 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() {} } The output of new B(); is: y=0

Problem with Dynamic Binding within Constructors class A { public A() {System.out.print(toString());} } class B extends A { private String s = Class B public String tostring() {return s.tolowercase();} } 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