Inclusion Polymorphism

Similar documents
Strict Inheritance. Object-Oriented Programming Spring 2008

Strict Inheritance. Object-Oriented Programming Winter

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

C++ Programming: Polymorphism

OBJECT ORIENTED PROGRAMMING USING C++

Polymorphism. Zimmer CSCI 330

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

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

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

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

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

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

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

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

Types. What is a type?

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

Inheritance and object compatibility

Inheritance and aggregation

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

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

Topic 9: Type Checking

Topic 9: Type Checking

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

The Four Polymorphisms in C++ Subtype Polymorphism (Runtime Polymorphism) Polymorphism in C

First IS-A Relationship: Inheritance

Object Oriented Paradigm

Inheritance, Polymorphism and the Object Memory Model

What are the characteristics of Object Oriented programming language?

Casting in C++ (intermediate level)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Concepts of Programming Languages

CPS 506 Comparative Programming Languages. Programming Language

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

TPF Users Group Spring 2005

Example: Count of Points

CSE 431S Type Checking. Washington University Spring 2013

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Chapter 5. Object- Oriented Programming Part I

More Relationships Between Classes

Tokens, Expressions and Control Structures

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

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

15: Polymorphism & Virtual Functions

What is Inheritance?

C++ Important Questions with Answers

CSI33 Data Structures

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

Increases Program Structure which results in greater reliability. Polymorphism

OBJECT ORİENTATİON ENCAPSULATİON

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

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

2.1 Why did C need a ++?

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

Object-oriented Programming. Object-oriented Programming

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

Introduction to Object-Oriented Programming

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

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

Casting and polymorphism Enumeration

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

Overloading המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

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

Lecture Notes on Programming Languages

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.

Example: Count of Points

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Modern Programming Languages. Lecture Java Programming Language. An Introduction

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Laboratorio di Tecnologie dell'informazione

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Instantiation of Template class

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Operators and Expressions

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Chapter 5 Object-Oriented Programming

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Fundamentals of Programming Languages

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

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

IS0020 Program Design and Software Tools Summer, 2004 August 2, 2004 in Class

Subtyping. Lecture 13 CS 565 3/27/06

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

G52CPP C++ Programming Lecture 13

Object typing and subtypes

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

25. Generic Programming

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

CS304 Object Oriented Programming

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

Improving Enumeration Types [N1513= ] David E. Miller

September 10,

Does anyone actually do this?

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

Chapter 2: Java OO II X I A N G Z H A N G


Transcription:

06D-1 Inclusion Polymorphism Polymorphic code Polymorphic references Up- and Down- Casting Polymorphism and values Polymorphic Arrays

Inclusion Polymorphism in Context 06D-2 Polymorphism Ad hoc Universal Coercion Overloading Inclusion Sub-type Parametric Code Objects Inclusion, or subtype polymorphism arises from inheritance.

Inclusion Polymorphism and OOP In Strict Inheritance: SubclassingSubtyping 06D-3 Suppose that the function fire() expects an instance of class. Then, it will also accept an instance of class, provided that (strictly) inherits from. has everything has (in exactly the same form!) The additional operations in will not be used in fire() We say that the type is a subtype of the type. We say that the type includes the type. The polymorphism in this case is a result of subtyping, or type inclusion.

Methods are Polymorphic E; M; raise_salary 06D-4 E.raise_salary(10); // OK is_manager_of M.raise_salary(10); // OK E.is_manager_of(...); // Error M.is_manager_of(E); // OK The code of the raise_salary method is Polymorphic. It can be applied to all subtypes (subclasses) of. We see that without polymorphism, inheritance makes very little sense.

Polymorphic Objects 06D-5 All variables in Smalltalk are polymorphic. They may store instances of all classes. this is a polymorphic object. It may point to things of different subtypes at different times. A pointer to an inherited type is generated whenever an inherited method is called. In fact, all class pointers and all class references in C++ are polymorphic...

Pointers as Polymorphic Objects 06D-6 E, *pe; M, *pm; pe pm E M Rules for pointer mixing: pe = &E; // OK - Ordinary C type rules pm = &M; // OK - Ordinary C type rules pe = &M; // OK - Pointers are polymorphic! pm = &E; // Compile time error

References as Polymorphic Objects 06D-7 E M ostream& operator << ( ostream &, const & ); E; M; &eref1 = E; // OK! &eref2 = M; // OK! Reference to subobject &mref1 = E; // Compile time error! &mref2 = M; // OK! cout << E << M; // OK! Reference to subobject

Up-Casting Casting: A synonym for coercion from a derived type to the base type. Up-casting: casting pointers up the inheritance hierarchy. Up-casting of this occurs implicitly whenever an inherited method is called. raise_salary 06D-8 E; M; is_manager_of M.is_manager_of(E); // Type of this is *. // No casting occurs. M.raise_salary(10); // Type of this is * // in raise_salary. // Up casting must occur.

Down-Casting Down-Casting: casting pointers and references down the inheritance hierarchy: Must be done explicitly. Done by experts, and only in special cases. 06D-9 pe E E, *pe; M M, *pm; pe = &M; // OK: implicit upcasting. M = *pe; // Error: implicit downcasting is not allowed // explicit down casting: M = *( *)pe; // deprecated syntax M = *static_cast<*>pe; // recommended syntax // Either way, you better know what you are doing!

Safe Down-Casting RTTI: Run Time Type Identification New ANSI C++ compilers support RTTI. A programmer can add RTTI using an is_a function Messy! Given a pointer, it is possible to determine its dynamic type. Usually it is not necessary, and an indication of a bad design 06D-10 if ( *p = dynamic_cast< *>pe) { p->is_manager_of(e); } &eref = M;... &mref = dynamic_cast< &>eref; // bad_cast exception will be thrown if cast is invalid.

Value Semantics, Coercion & Polymorphism 06D-11 Polymorphism is applicable to code and variables but not to values. Coercion: translation from a value of one type to a value of a different type. Often with some loss of contents. Example: coercion from integer to real and vice versa. Inheritance in C++: defines a coercion from the derived class to the base class. The coercion is parametric! It works for all subtypes! Coercion is done by extracting the subobject.

What are Subobjects? 06D-12 class Base { //... }; class Derived: public Base { //... }; Each object of class Derived has a subobject of class Base It is possible in many cases to relate to that subobject

E M Mixing Values 06D-13 E; M; Rules for Mixing Values 1. call the (compiler generated) type casting operator from to 2. call the (compiler-defined or user-defined) to assignment operator. E = M; // OK - Valid coercion: truncation will occur. M = E; // Error - No coercion is defined. // is an but not vice-versa!

Arrays of Values 06D-14 Department[10]; Management[10]; Department and Management are not compatible in any way. In general, sizeof <= sizeof Usually, sizeof < sizeof Therefore, an array of managers (usually) occupies more space than an array of the same size of employees, and the conversion between the two is not trivial. This is just like an array of char which is not compatible at all with an array of int, although char and int are compatible in some operations.

Arrays of Pointers It is often convenient to define mixed type collections. The simplest and easiest way to do so is to use an array of pointers to the base class. 06D-15 *Department[100]; It is easy to deposit objects into the above array, however, determining the type of object that resides in a certain location requires down casting. Down casting should be used only in extremely special cases. In this common situation, what should be used instead is dynamic binding which is to be discussed in shortly.