C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

Similar documents
Object typing and subtypes

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

C++ Yanyan SHEN. slide 1

Subtyping (Dynamic Polymorphism)

History C++ Design Goals. How successful? Significant constraints. Overview of C++

Objects, Encapsulation, Inheritance (2)

G Programming Languages - Fall 2012

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

Data Abstraction. Hwansoo Han

Simula 67. Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula

Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula. John Mitchell

References: Chapters 10 and 11 Chapters 8, and 12( 2 and 3)

Some instance messages and methods

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

The Java Programming Language

What about Object-Oriented Languages?

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Procedure and Object- Oriented Abstraction

Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

Inheritance Considered Harmful

CS 520 Theory and Practice of Software Engineering Fall 2018

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

Inheritance. Transitivity

CS 320 Introduction to Software Engineering Spring March 06, 2017

Implementing Higher-Level Languages. Quick tour of programming language implementation techniques. From the Java level to the C level.

Object-oriented Programming. Object-oriented Programming

CS153: Compilers Lecture 11: Compiling Objects

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

Object Oriented Languages. Hwansoo Han

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Special error return Constructors do not have a return value What if method uses the full range of the return type?

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Compiler construction 2009

Mid-Term 2 Grades

CS 520 Theory and Practice of Software Engineering Fall 2017

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

CS558 Programming Languages Winter 2013 Lecture 8

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Exceptions and Continuations. Lecture #19: More Special Effects Exceptions and OOP. Approach II: Non-Standard Return. Approach I: Do Nothing

Programming Languages and Techniques (CIS120)

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CS558 Programming Languages

Concepts of Programming Languages

Conformance. Object-Oriented Programming Spring 2015

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Programming Languages 2nd edition Tucker and Noonan"

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

Inheritance, Polymorphism, and Interfaces

CSE 401 Final Exam. December 16, 2010

Overriding Variables: Shadowing

C++ Programming: Polymorphism

CSE 401/M501 Compilers

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Polymorphism. Zimmer CSCI 330

C++ Inheritance and Encapsulation

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Introduction to Programming (Java) 4/12

What is a type? Types in OO languages. What is a type? Types as sets. Example. Clients of screensaver

CS24 Week 4 Lecture 2

Programming Languages: OO Paradigm, Polymorhism and Class Members

Imperative Programming 2: Inheritance 1

Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

C++ Inheritance and Encapsulation

Recap. What is a type? Types in OO languages. What is a type? Types as sets. Up next, some advanced OO topics

CS250 Final Review Questions

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

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

Konzepte von Programmiersprachen

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

VIRTUAL FUNCTIONS Chapter 10

Programming Languages and Techniques (CIS120)

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Stacks have many uses Arithmetic Language parsing Keeping track of recursion (more in this in a week or so)

Chapter 1: Object-Oriented Programming Using C++

CS18000: Programming I

CS422 - Programming Language Design

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 4, 2016

Inheritance and object compatibility

Inheritance. Inheritance

Short Notes of CS201

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

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

index.pdf January 21,

Java Object Oriented Design. CSC207 Fall 2014

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

CS201 - Introduction to Programming Glossary By

Abstract Data Types. Abstract Data Types

Programming Languages and Techniques (CIS120)

Transcription:

C++ Run-Time Representation Point object Point vtable Code for move C++ Part x y CSCI 4 Stephen Freund ColorPoint object x 4 y 5 c red ColorPoint vtable Code for move Code for darken Data at same offset Function pointers at same offset! C++:Point Point *pt = ; pt -> move(,10); pt must be a Point or a subclass of Point All subclasses of Point use same vtable order for Point methods. Compiler can hard-code in vtable offset: (pt->vtable[0])(pt,, 10)! Smalltalk: pt movedx: Dy: 10 pt could be any object with movedx:dy: in protocol No info about layout. Must search dictionary. (super class) Point object Point class Template x y Method dictionary newx:y: draw movedx:dy: Could we put methods at same offset? Class A def movedx:dy: Class C Class B def movedx:dy: def print def print! Static type info about object layout more efficient lookup but less expressive " unrelated classes cannot be subtypes! Things to think about: How Java interface declarations fit into this picture? How do Scala traits? 1

Non-Virtual Methods int getx(); virtual void move(int dx, int dy); ; Point *pt = ; pt -> getx(); Compiler hard-codes in direct jump: Point::getX(pt,, 10)! Why useful? class InputStream { virtual int read(); InputStream virtual String readline(); virtual int readint(); virtual long readlong(); FileInputStream SocketInputStream ; ConsoleInputStream String InputStream::readLine() { String result = ""; while (true) { int ch = read(); if (ch == '\n') break; result += ch; return result; class InputStream { virtual int read(); String readline(); int readint(); long readlong(); ; String InputStream::readLine() { String result = ""; while (true) { int ch = read(); if (ch == '\n') break; result += ch; return result; FileInputStream InputStream ConsoleInputStream SocketInputStream class FileInputStream : public InputStream { virtual int read() { ; class SocketInputStream : public InputStream { virtual int read() { ; Overloading vs. Virtual Methods void printclass() { cout << "Point"; virtual void printclassvirtual() { cout << "Point"; ; void printclass() { cout << "ColorPoint"; virtual void printclassvirtual() { cout << "ColorPoint"; ; Point *p = new Point(); p->printclassvirtual(); p->printclass(); cp->printclassvirtual(); cp->printclass(); Overloading vs. Virtual Methods void printclass() { cout << "Point"; virtual void printclassvirtual() { cout << "Point"; ; void printclass() { cout << "ColorPoint"; virtual void printclassvirtual() { cout << "ColorPoint"; ; Point *p = new ColorPoint(); p->printclassvirtual(); p->printclass();

Overloading Based on Arguments class OutputStream { void println(int v); void println(double d); void println(string s); void println(object o); OutputStream *out = ; out->println(); out->println("moo"); Overriding vs. Overloading (Can be painful to think about) virtual boolean equals(point *p) // 1 { ; virtual boolean equals(point *p) // { Object *obj = "cow"; out->println(obj); // String <: Object ; virtual boolean equals(colorpoint *cp) // { Point *p1 = new Point(); Point *p = new ColorPoint(); Point *p1 = new Point(); Point *p = new ColorPoint(); p1->equals(p1); p1->equals(p); p1->equals(cp); p1->equals(p1); p1->equals(p); p1->equals(cp); p->equals(p1); p->equals(p); p->equals(cp); p->equals(p1); p->equals(p); p->equals(cp); cp->equals(p1); cp->equals(p); cp->equals(cp); cp->equals(p1); cp->equals(p); cp->equals(cp); Different Notions of Subtyping! Smalltalk protocol conformance A <: B if protocol of B protocol of A! Java A <: B if A extends B A <: I if A implements I! C++ A <: B if B is public base class of A class A : public B { è A <: B class A : private B { è A <: B Private vs Public Base Classes class DEBuffer { void addfirst(int v); void addlast(int v); int removefirst(); int removelast(); int elems[]; int size; class Stack : private DEBuffer { void push(int v) { addlast(v); int pop() { return removelast(); void invert() { class Queue : private DEBuffer { void enqueue(int v) { int dequeue() {

Why Not Just Use Delegation? Abstract Base Classes class DEBuffer { void addfirst(int v); void addlast(int v); int removefirst(); int removelast(); int elems[]; int size; class Stack { private: DEBuffer *delegate; void push(int v) { delegate->addlast(v); int pop() { return delegate->removelast(); void invert() { class Expr { private: Type t; virtual int eval() = 0; virtual int leaves() { return 0; class Plus : public Expr { private: Expr *left; Expr *right; virtual int eval() { return left->eval() + right->eval(); Expr *e = new Plus(); // OK Expr *f = new Expr(); // BAD virtual Point *copy() { virtual ColorPoint *copy() { Point *copy = pt->copy();! f1 : int # Point! f : int # ColorPoint! f1 : Point # int! f : ColorPoint # int 4

! f1 : ColorPoint # int! f : Point # int! Okay when C <: A and B <: D ColorPoint # Point ColorPoint # ColorPoint Point # Point Point # ColorPoint virtual Point *copy() { virtual ColorPoint *copy() { Point *copy = pt->copy(); yes virtual boolean equals(point *pt) { virtual boolean equals(colorpoint *cpt) { ColorPoint *cpt; Point *pt = cpt; if (pt -> equals(pt)) { Assume this overrides def from superclass no Stack Allocated Objects void m() { Point *ref = new Point(1,1); Point pt(,); ColorPoint cpt(4,5,red); control link ref->move(5,5); pt.move(10,10); cpt.getcolor(); delete ref; pt cpt ref x y x 4 y 5 col red x 1 y 1 Point vtable CPoint vtable 5