08 - Polymorphism Click to edit Master subtitle style...

Info icon This preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style Chapter Eight Modern Programming Languages, 2nd ed. Polymorphism 11
Image of page 1

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

View Full Document Right Arrow Icon
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 22 int f(char a, char b) { return a==b; } - fun f(a, b) = (a = b); val f = fn : ''a * ''a -> bool ML: C:
Image of page 2
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 33
Image of page 3

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

View Full Document Right Arrow Icon
Chapter Eight Modern Programming Languages, 2nd ed. Outline Overloading Parameter coercion Parametric polymorphism Subtype polymorphism Definitions and classifications 44
Image of page 4
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 55
Image of page 5

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

View Full Document Right Arrow Icon
Chapter Eight Modern Programming Languages, 2nd ed. Predefined Overloaded Operators 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;
Image of page 6
Chapter Eight Modern Programming Languages, 2nd ed. Adding to Overloaded Operators Some languages, like C++, allow additional meanings to be defined for operators 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; }
Image of page 7

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

View Full Document Right Arrow Icon
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) ) 88
Image of page 8
Chapter Eight Modern Programming Languages, 2nd ed. Defining Overloaded Functions Some languages, like C++, permit the programmer to overload function names 99 int square(int x) { return x*x; } double square(double x) { return x*x; }
Image of page 9

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

View Full Document Right Arrow Icon
Chapter Eight Modern Programming Languages, 2nd ed. To Eliminate Overloading 1010 int square(int x) { return x*x; } double square(double x) { return x*x; } void f() { int a = square(3); double b = square(3.0); } You could rename each overloaded definition uniquely… square_i square_d
Image of page 10
Chapter Eight Modern Programming Languages, 2nd ed. How To Eliminate Overloading 1111 int square_i(int x) { return x*x; } double square_d(double x) { return x*x; } void f() { int a = square_i(3); double b = square_d(3.0); } Then rename each reference properly (depending on the parameter types)
Image of page 11

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

View Full Document Right Arrow Icon
Chapter Eight Modern Programming Languages, 2nd ed.
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern