Docsity

Polymorphism - Modern Programming Languages - Lecture Slides, Slides of Advanced Computer Programming

Partial preview of the text

Download Polymorphism - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

Polymorphism

Chapter Eight Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Introduction

 Compare these function types

 The ML function is more flexible, since it

can be applied to any pair of the same

(equality-testable) type

Chapter Eight Modern Programming Languages, 2nd ed. 2

int f(char a, char b) { return a==b; }

- fun f(a, b) = (a = b); val f = fn : ''a * ''a -> bool

ML:

C:

Docsity.com

Outline

 Overloading

 Parameter coercion

 Parametric polymorphism

 Subtype polymorphism

 Definitions and classifications

Chapter Eight Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Overloading

 An overloaded function name or operator is

one that has at least two definitions, all of

different types

 Many languages have overloaded operators

 Some also allow the programmer to define

new overloaded function names and

operators

Chapter Eight Modern Programming Languages, 2nd ed. (^) Docsity.com 5

Adding to Overloaded Operators

 Some languages, like C++, allow additional

meanings to be defined for operators

Chapter Eight Modern Programming Languages, 2nd ed. 7

class complex { double rp, ip; // real part, imaginary part public: complex(double r, double i) {rp=r; ip=i;} friend complex operator+(complex, complex); friend complex operator(complex, complex); };*

void f(complex a, complex b, complex c) { complex d = a + b * c; … }

Docsity.com

Operator Overloading In C++

 C++ allows virtually all operators to be

overloaded, including:

  • the usual operators ( + , - , ***** , / , % , ^ , & , | , ~ ,! , = , < , > ,

+= , -=,= , *= , /= , %= , ^= , &= , |= , << , >> , >>= , <<= , == ,

  • dereferencing ( *p and p->x )
  • subscripting ( a[i] )
  • function call ( f(a,b,c) )
  • allocation and deallocation ( new and delete )

Chapter Eight Modern Programming Languages, 2nd ed. (^) Docsity.com 8

To Eliminate Overloading

Chapter Eight Modern Programming Languages, 2nd ed. 10

int square(int x) { return xx; }*

double square(double x) { return xx; }*

void f() { int a = square(3); double b = square(3.0); }

You could rename each overloaded definition uniquely…

square_i

square_d

Docsity.com

How To Eliminate Overloading

Chapter Eight Modern Programming Languages, 2nd ed. 11

int square_i(int x) { return xx; }*

double square_d(double x) { return xx; }*

void f() { int a = square_i(3); double b = square_d(3.0); }

Then rename each reference properly (depending on the parameter types)

Docsity.com

Example: C++ Implementation

Chapter Eight Modern Programming Languages, 2nd ed. 13

int shazam(int a, int b) {return a+b;} double shazam(double a, double b) {return a+b;}

shazam__Fii: lda $30,-32($30) .frame $15,32,$26, … shazam__Fdd: lda $30,-32($30) .frame $15,32,$26, …

C++:

Assembler:

Docsity.com

Outline

 Overloading

 Parameter coercion

 Parametric polymorphism

 Subtype polymorphism

 Definitions and classifications

Chapter Eight Modern Programming Languages, 2nd ed. (^) Docsity.com 14

Parameter Coercion

 Languages support different coercions in

different contexts: assignments, other binary

operations, unary operations, parameters…

 When a language supports coercion of

parameters on a function call (or of

operands when an operator is applied), the

resulting function (or operator) is

polymorphic

Chapter Eight Modern Programming Languages, 2nd ed. (^) Docsity.com 16

Example: Java

Chapter Eight Modern Programming Languages, 2nd ed. 17

void f(double x) { … }

f((byte) 1); f((short) 2); f('a'); f(3); f(4L); f(5.6F);

This f can be called with any type of parameter Java is willing to coerce to type double

Docsity.com

Example: Java

Chapter Eight Modern Programming Languages, 2nd ed. 19

Some operators apply unary numeric promotion to a single operand, which must produce a value of a numeric type: If the operand is of compile-time type Byte , Short , Character , or Integer it is subjected to unboxing conversion. The result is then promoted to a value of type int by a widening conversion or an identity conversion. Otherwise, if the operand is of compile-time type Long , Float , or Double it is subjected to unboxing conversion. Otherwise, if the operand is of compile-time type byte , short , or char , unary numeric promotion promotes it to a value of type int by a widening conversion. Otherwise, a unary numeric operand remains as is and is not converted. In any case, value set conversion is then applied. Unary numeric promotion is performed on expressions in the following situations:

  • Each dimension expression in an array creation expression
  • The index expression in an array access expression
  • The operand of a unary plus operator +
  • The operand of a unary minus operator -
  • The operand of a bitwise complement operator ~
  • Each operand, separately, of a shift operator >> , >>> , or << ; therefore a long shift distance (right operand) does not promote the value being shifted (left operand) to The Java Language Specification, Third Edition long … James Gosling, Bill Joy, Guy Steele, and Gilad Bracha

Docsity.com

Coercion and Overloading:

Tricky Interactions

 There are potentially tricky interactions

between overloading and coercion

  • Overloading uses the types to choose the

definition

  • Coercion uses the definition to choose a type

conversion

Chapter Eight Modern Programming Languages, 2nd ed. (^) Docsity.com 20