polymorphism cmps 2143. poly-morphism means “many-forms” means different things in biology,...

9
Polymorphism CMPS 2143

Upload: earl-grant

Post on 19-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

Polymorphism

CMPS 2143

Page 2: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

2

Poly-morphism

•Means “many-forms”•Means different things in biology, chemistry, computer

science•Means different things to functional language users and

OOP language users

•Means there is ONE name, many different meanings▫Names: variables, functions, methods, class names▫Meanings: can be defined in different ways

Page 3: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

3

Four forms of polymorphism

•Overloading•Overriding•Polymorphic variables•Generics (templates)

Page 4: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

4

Overloading – More in Chapter 15

•AKA ad-hoc polymorphism•Single function/method name has several alternative

implementations•Distinguished at compile time by type signature

class myClass { public: //3 overloaded meanings for same name void example (int x) {…} void example (int x, double y) {…} void example (string x) {…}}

Page 5: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

5

Overriding – More in Chapter 16

•Single function/method name has several alternative implementations with the same signature, but occurs within context of the parent class/child class relationship

class Parent { public: void example (int x) {…}}class Child { public: //same name, different method body void example (int x) {…}}

Page 6: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

6

Polymorphic Variable – More in Chapter 17

•AKA assignment polymorphism•Variable is declared of one type, but holds a value of a

different type

//declared as parent, holding child value Room r = new King(…);

•When a polymorphic variable is used as an argument, the result function/method is said to exhibit pure polymorphism.

Page 7: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

7

Generics/Templates – More in Chapter 18

•A way of creating general purpose tools and specializing them to specific situations

template <class T> T max (T left, T right){ if (left < right) return right; return left; }

•A generic function or class is parameterized by type ▫Similar to the way a function is parameterized by values

•The type is unspecified, to be filled in later

Page 8: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

8

Many tools, one goal

•Polymorphism is the feature of OOP languages that distinguish them the most from other types of languages

•Each different polymorphic technique permits a different form of reuse

Page 9: Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional

9

Study questions

•Pg. 286: 2-5, 9