CSCI 383 Object-Oriented Programming & Design Lecture 21 Martin van Bommel.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
OOP Languages: Java vs C++
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
CSCI-383 Object-Oriented Programming & Design Lecture 23.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
OOP Basics Classes & Methods (c) IDMS/SQL News
Polymorphism Lecture - 9.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Function Overloading.
Types of Programming Languages
Lecture 6 C++ Programming
CMSC 202 Lesson 22 Templates I.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Overloading
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Templates I CMSC 202.
CS410 – Software Engineering Lecture #5: C++ Basics III
C Data Types and Variable
Templates CMSC 202, Version 4/02.
C++ Object Oriented 1.
Presentation transcript:

CSCI 383 Object-Oriented Programming & Design Lecture 21 Martin van Bommel

Fall 2010CSCI 383 Lecture 21 M. van Bommel 2 Polymorphism Polymorphous: Having, or assuming various forms, characters, or styles From greek roots, poly = many, and Morphos = form (Morphus was the greek god of sleep, who could assume many forms, and from which we derive the name Morphine, among other things) In OOP, polymorphism is when variables or functions have more than one form

Fall 2010CSCI 383 Lecture 21 M. van Bommel 3 Polymorphic Variables Polymorphic variables, or untyped variables, are variables that have no type associated with them Rather, type is associated with the data stored in the variables. Thus, the variable can hold values of different types during the course of execution LISP & Smalltalk directly support polymorphic variables The most common polymorphic variable is the one that holds the receiver during the execution of a method Called this in C++ and Java, self in Smalltalk and Objective-C current in Eiffel

Fall 2010CSCI 383 Lecture 21 M. van Bommel 4 Polymorphic Variables C++ provides limited support for polymorphic variables in the form of class pointer conversions C++ permits one to treat base classes as compatible abstractions of derived classes This allows one to store the address of a derived class instance in a base class pointer variable Shape *ptr = new Circle;

Fall 2010CSCI 383 Lecture 21 M. van Bommel 5 Polymorphism & Typing Polymorphic Code (functional polymorphism) may be invoked with variables of different type (writing almost at a pseudo-code level) Dynamically Typed Languages: code is polymorphic (almost by definition) Statically Typed Languages: code restricted to declared type of variables (a priori) Main challenge: polymorphism in statically typed languages Expressive power Safety

Fall 2010CSCI 383 Lecture 21 M. van Bommel 6 Functional Polymorphism Functional polymorphism exists when a function or procedure has more than one form Two types of functional polymorphism are Pure polymorphism Ad hoc polymorphism

Fall 2010CSCI 383 Lecture 21 M. van Bommel 7 Pure Polymorphism A polymorphic method (pure polymorphism) occurs when a polymorphic variable is used as an argument Different effects, which vary depending upon the argument, are formed by using different types of value

Fall 2010CSCI 383 Lecture 21 M. van Bommel 8 Ad hoc Polymorphism Ad hoc: 1. For the specific purpose, case, or situation at hand and for no other: a committee formed ad hoc to address the issue of salaries 2. Formed for or concerned with one specific purpose: an ad hoc compensation committee 3. Improvised and often impromptu: On an ad hoc basis, Congress has placed ceilings on military aid to specific countries

Fall 2010CSCI 383 Lecture 21 M. van Bommel 9 Ad hoc Polymorphism Polymorphism is over finitely few shapes Often, very few Different shapes are generated manually, or semi- manually No unifying common ground to all shapes, other than designer’s intentions Uniformity is a coincidence, not a rule

Fall 2010CSCI 383 Lecture 21 M. van Bommel 10 Overloading Polymorphism User or system overloads an identifier or operator to work with different types E.g., C++’s user-defined overloading of the function name max double max(double d1, double d2); char max(char c1, char c2); char* max(char* s1, char* s2); E.g., C++’s user-defined overloading of the += operator class Rational { public: Rational(double); const Rational& operator+=(const Rational& other);... };

Fall 2010CSCI 383 Lecture 21 M. van Bommel 11 A Definition of Overloading We say a term is overloaded if it has two or more meanings Most words in natural languages are overloaded, and confusion is resolved by means of context Same is true of OO languages There are two important classes of context that are used to resolve overloaded names Overloading based on scopes Overloading based on type signatures

Fall 2010CSCI 383 Lecture 21 M. van Bommel 12 Interclass Overloading Interclass overloading exists when different implementations of a function exist in different classes (or scopes) In this case, the fully-scoped name of the function is sufficient to distinguish it from other implementations with the same function name

Fall 2010CSCI 383 Lecture 21 M. van Bommel 13 Intraclass Overloading Intraclass overloading exists when different implementations of a function exist within the same class (or scope) In C++, implementations are distinguished from one another by their fully-scoped names and their signatures

Fall 2010CSCI 383 Lecture 21 M. van Bommel 14 Distinguishing C++ Functions The following rules are used to distinguish functions from one another in C++ A function call first chooses from among all functions of that name, within the scope specified, those for which the argument types match the parameter types or for which a set of conversions exist The set of functions that best match the call is calculated; if the set has more than one element, ambiguity exists

Fall 2010CSCI 383 Lecture 21 M. van Bommel 15 Distinguishing C++ Functions void setAngle(int degrees) void setAngle(float radians) int i = 45; float f = ; setAngle(i); // Okay setAngle(f); // Okay setAngle( ); // OOPS!

Fall 2010CSCI 383 Lecture 21 M. van Bommel 16 Stream Output in C++ Stream output is a good example of the power of overloading. Every primitive type has a different stream output function ostream& operator<<(ostream& destination, int source); ostream& operator<<(ostream& destination, short source); ostream& operator<<(ostream& destination, long source); ostream& operator<<(ostream& destination, char source); ostream& operator<<(ostream& destination, char* source); //... and so on

Fall 2010CSCI 383 Lecture 21 M. van Bommel 17 Coercion Polymorphism Polymorphism arising from the existence of built-in or user-defined coercions between types int pi = ; // Built-in coercion from double to int float x = ’\0’; // Built-in coercion from char to float extern double sqrt(double); x = sqrt(pi); // Built-in coercion from int to double and // built-in coercion from double to float

Fall 2010CSCI 383 Lecture 21 M. van Bommel 18 Coercion Polymorphism class Rational { public: Rational(double); operator double(void);... }; Rational r(2); // Built-in coercion from int to double // and user-defined coercion from double // to Rational cout << sqrt(r); // User-defined coercion from Rational // to double (also C++’s // overloading of the << operator)