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

Similar documents
C++ Yanyan SHEN. slide 1

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

Object typing and subtypes

Subtyping (Dynamic Polymorphism)

Objects, Encapsulation, Inheritance (2)

The Java Programming Language

CS153: Compilers Lecture 11: Compiling Objects

Data Abstraction. Hwansoo Han

CS-202 Introduction to Object Oriented Programming

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

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

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Java Object Oriented Design. CSC207 Fall 2014

Building custom components IAT351

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

Conformance. Object-Oriented Programming Spring 2015

G Programming Languages - Fall 2012

Design Patterns: State, Bridge, Visitor

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

Procedure and Object- Oriented Abstraction

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

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

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

CSE 307: Principles of Programming Languages

Chapter 10 :: Data Abstraction and Object Orientation

Chapter 5 Object-Oriented Programming

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

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

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

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

Object Oriented Software Design II

Overriding Variables: Shadowing

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Object Oriented Software Design II

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

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

CSE 401/M501 Compilers

CS558 Programming Languages Winter 2013 Lecture 8

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

What about Object-Oriented Languages?

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Lecturer: William W.Y. Hsu. Programming Languages

What is Inheritance?

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

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

C++ Important Questions with Answers

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

What are the characteristics of Object Oriented programming language?

COMP6771 Advanced C++ Programming

QUIZ. How could we disable the automatic creation of copyconstructors

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

ITI Introduction to Computing II

CSE 431S Type Checking. Washington University Spring 2013

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

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

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Introducing C++ David Chisnall. March 15, 2011

QUIZ. How could we disable the automatic creation of copyconstructors

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

ITI Introduction to Computing II

Polymorphism Part 1 1

Chapter 9 :: Data Abstraction and Object Orientation

Chapter 10 Object-Oriented Programming

Some instance messages and methods

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

Binghamton University. CS-140 Fall Dynamic Types

CPS 506 Comparative Programming Languages. Programming Language

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Inheritance, Polymorphism and the Object Memory Model

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

Lecture Notes on Programming Languages

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

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

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

COP 3330 Final Exam Review

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

Introduction to Inheritance

Inheritance and Interfaces

CS 320 Introduction to Software Engineering Spring March 06, 2017

CS 162, Lecture 25: Exam II Review. 30 May 2018

VIRTUAL FUNCTIONS Chapter 10

Chapter 14 Abstract Classes and Interfaces

Classes and Inheritance Extending Classes, Chapter 5.2

Introduction to C++ with content from

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

C++ Programming: Polymorphism

C++ Inheritance and Encapsulation

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

CMSC 132: Object-Oriented Programming II. Inheritance

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

ECE 3574: Dynamic Polymorphism using Inheritance

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Late binding Ch 15.3

CSE351 Winter 2016, Final Examination March 16, 2016

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

Transcription:

C++ & Subtyping

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

Why talk about C++? C++ is an OO extension of C Efficiency and flexibility from C OO program organization from Simula Interesting design decisions Features were and still are added incrementally Backwards compatibility is a huge priority What you don t use, you don t pay for. - Bjarne Stroustrup

Recall: C++ OO concepts in 1 slide Encapsulation Public, private, protected + friend classes Dynamic lookup Only for special functions: virtual functions Inheritance Single and multiple inheritance! Public and private base classes! Subtyping: tied to inheritance

Plan for C++ Look at dynamic lookup as done in C++ (vtables) Why? Only interesting when inheritance comes into play Why?

Simple example runtime representation of A object class A { int a; void f(int); A* pa; pa->f(2); compiles to info necessary to lookup function: type of pointer int a A_f(pa, 2);

Inheritance class A { int a; void f(int); class B : A { int b; void g(int) class C : B { int c; void h(int) Does runtime representation of A objects change? A: yes, B: no

Inheritance class A { int a; void f(int); class B : A { int b; void g(int) class C : B { int c; void h(int) runtime representation of B object int a int b runtime representation of C object int a int b int c

Inheritance + virtual methods class A { int a; virtual void f(int); virtual void g(int); virtual void h(int); class B : A { int b; void g(int) class C : B { int c; void h(int) C* pc; pc->g(2); pc compiles to info necessary to lookup function: found at runtime runtime representation of C object vptr int a int b int c vtable A::f B::g C::h (*(pc->vptr[1]))(pc, 2)

Non-virtual vs. Virtual Non-virtual functions Do they get called directly? A: yes, B: no Virtual functions Do they get called directly? A: yes, B: no They go through the vtable

Non-virtual vs. Virtual Non-virtual functions Can they be redefined? A: yes, B: no, C: ehhhh They can be overloaded Virtual functions Can they be redefined? A: yes, B: no, C: ehhhh

Virtual methods can be redefined class A { int a; virtual void f() { printf( parent ); class B : A { int b; virtual void f() { printf( child ); A* pa = new B(); pa->f(); pa compiles to vptr int a int b info necessary to lookup function: found at runtime runtime representation of B object vtable B::f (*(pa->vptr[0]))(pa)

Non-virtual functions are overloaded class A { int a; void f() { printf( parent ); class B { int b; void f() { printf( child ); A* pa = new B(); pa->f(); pa compiles to int a int b info necessary to lookup function: type of pointer runtime representation of B object A_f(pa)

Dynamic vs. static OO systems Smalltalk and JavaScript: no static type system In message obj.method(arg), the obj can refer to anything Need to find method using pointer from obj The location in dictionary/hashtable will vary In C++ compiler knows the superclass for obj Offset of data and function pointers are the same in subclass and superclass Invoke function pointer at fixed offset in vtable!

Virtual method call takeaway Invoke function pointer at fixed offset in vtable!

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

What is subtyping? Relationship between interfaces in contrast to inheritance: relationship between implementations If interface A contains all of interface B, then A <: B Interface = set of messages the object understands Eg., ColorPoint <: Point

Subtyping in JavaScript Objects implicitly have an interface No recorded by some type system; Point {x, y, move ColoredPoint {x, y, move, color No relationship to inheritance can delete methods, etc. Boo {x, y, move, boo

Subtyping in C++ Is implementing same functions enough? A: yes, B: no class ColoredPoint { public: virtual void move(); virtual int color(); private:... class Point { public: virtual int move(); private:... Subtyping is explicit A <: B if A has public base class B

What is an interface in C++? Recall: everything gets compiled down to fn call memory layout of objects memory layout of vtables From inheritance, we get: compatible memory layout subtype relation

What does subtyping really mean?

Where does the name come from? ColoredPoint vs. Point Interface is clearly bigger for Colored Point Point {x, y, move ColoredPoint {x, y, move, color Why subtype? Think: Natural <: Integer Think: Points ColoredPoints

What does it mean in PL? S is a subtype of T if any term of type S can be used in a context where a term of type T is expected This is a runtime phenomenon: when one term can be used where an object of another type is expected Static type system can tell us if we got it right

What does it mean in PL? e :: S S <: T e :: T

Who defines <:? Language designers! How is <: defined in C++? Class definition: class B: public A { tells us B <: A Why is the definition important? It may restrict how we can override functions in subclasses

Who defines <:? A: developers B: language designers

Who defines <:? How is <: defined in C++? Class definition: class B: public A { tells us B <: A Why is the definition important? It may restrict how we can override functions in subclasses

Return covariance Is it OK to override clone as follows? class A { public: virtual bool equals(a&); virtual A* clone(); class B: public A { public: bool equals(a&); B* clone(); Yes! Why? any case we need clone of As, we can use B s clone and upcast the B to an A. Suppose A* pa = new B(); then pa->clone(); returns a B* that can always be casted to an A*

Argument covariance Is it OK to override equals as follows? class A { public: virtual bool equals(a&); virtual A* clone(); class B: public A { public: bool equals(b&); B* clone(); No! Why? the implementation of equals must be prepared for any object of type A to be passed in; B is one kind of A Suppose A* pa1 = new B(); and A* pa2 = new C(); where C is a subclass of A then pa->equals(pa2); should fail since we should not be allowed to cast a C object to a B object

Subtyping rule for functions Subtyping for function results if A <: B then C -> A <: C -> B (covariance) E.g., C -> ColorPoint <: C -> Point Anywhere you expect a function that returns a B you can use a function that returns an A A s are B s so you can upcast the return value

Subtyping rule for functions Subtyping for function arguments if A <: B then B -> C <: A -> C (contravariance) E.g., Point -> C <: ColorPoint -> C Anywhere you expect a function that can operate As you can use a more general function that operates on Bs you can always cast the A argument you were going to call the function with to a B

Return covariance (w/ records) class A { public: virtual bool equals(a&); virtual A* clone(); class B: public A { public: bool equals(a&); B* clone(); type A = { equals :: A -> bool; clone :: () -> A; <: <: type B = { equals :: A -> bool; clone :: () -> B;

Argument covariance (w/ records) class A { public: virtual bool equals(a&); virtual A* clone(); class B: public A { public: bool equals(b&); B* clone(); type A = { equals :: A -> bool; clone :: () -> A; <: <: type B = { equals :: B -> bool; clone :: () -> B;

Example Circle <: Shape <: Circle -> Shape <: Circle -> Circle Shape -> Shape <: <: Shape -> Circle

For other data types: can be tricky! E.g., Java screwed up <: definition for Arrays Generic arrays are covariant Breaks type and memory safety!

We are placing trust in <:

Today Statically-typed OO languages: C++ vtables Closer look at subtyping