08 - Click to edit Master subtitle style Chapter Eight...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Eight Modern Programming Languages, 2nd ed. Polymorphism Chapter Eight Modern Programming Languages, 2nd ed. 11 Chapter Eight Modern Programming Languages, 2nd ed. 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. 22 int f(char a, char b) { return a==b; }- fun f(a, b) = (a = b); val f = fn : ''a * ''a -> bool ML: C: Chapter Eight Modern Programming Languages, 2nd ed. Polymorphism ■ Functions with that extra flexibility are called polymorphic ■ A difficult word to define: – Applies to a wide variety of language features – Most languages have at least a little – We will examine four major examples, then return to the problem of finding a definition that covers them Chapter Eight Modern Programming Languages, 2nd ed. 33 Chapter Eight Modern Programming Languages, 2nd ed. Outline ■ Overloading ■ Parameter coercion ■ Parametric polymorphism ■ Subtype polymorphism ■ Definitions and classifications Chapter Eight Modern Programming Languages, 2nd ed. 44 Chapter Eight Modern Programming Languages, 2nd ed. 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. 55 Chapter Eight Modern Programming Languages, 2nd ed. Predefined Overloaded Operators Chapter Eight Modern Programming Languages, 2nd ed. 66 Pascal: a := 1 + 2; b := 1.0 + 2.0; c := "hello " + "there"; d := ['a'..'d'] + ['f'] ML: val x = 1 + 2; val y = 1.0 + 2.0; Chapter Eight Modern Programming Languages, 2nd ed. Adding to Overloaded Operators ■ Some languages, like C++, allow additional meanings to be defined for operators Chapter Eight Modern Programming Languages, 2nd ed. 77 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; … } Chapter Eight Modern Programming Languages, 2nd ed. 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) ) Chapter Eight Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 39

08 - Click to edit Master subtitle style Chapter Eight...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online