CS 2130 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CS 2130

Description:

Webster's Dictionary: poly-mor-phic: able to assume different forms. ... case FLAUTIST: printf('flautist plays: tweetn'); break; Parametric polymorphism. or ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 22
Provided by: ble87
Category:
Tags: flautist

less

Transcript and Presenter's Notes

Title: CS 2130


1
CS 2130
  • Presentation 32
  • Polymorphism

2
What is polymorphism?
  • Webster's Dictionary poly-mor-phic able to
    assume different forms.

3
What is polymorphism?
  • Appel A function is polymorphic (from the Greek
    manyshape) if it can operate on arguments of
    different types. There are two main kinds of
    polymorphism
  • Parametric polymorphism. A function is
    parametrically polymorphic if it follows the same
    algorithm regardless of the type of its argument.
    (The Ada or Modula-3 generic mechanism , C
    templates, or ML type schemes are examples)
  • Overloading. A function identifier is overloaded
    if it stands for different algorithms depending
    on the type of its argument. (Example sign same
    for floatsints

Modern Compiler Implementation in C
4
What is polymorphism?
  • Loudon A language is polymorphic if it allows
    language constructs to have more than one type.
  • Monomorphism names expressions have unique
    types
  • Overloading Form of polymorphism where separate
    declarations are allowed for the same name

5
What is polymorphism?
  • Sebesta "The third characteristic of
    object-oriented programming languages is a kind
    of polymorphism provided by the dynamic binding
    of messages to method definitions."
  • Allows one to define polymorphic variables of the
    type of the parent class that are also able to
    reference objects of any of the subclasses of
    that class.
  • The parent and child class can both have a method
    with the same name. At run time the appropriate
    method is called depending on the type of the
    object.

Concepts of Programming Languages 4th Edition
6
Example
  • Polymorphism in C
  • define MAX(a,b) ((a) gt (b) ? (a) (b))
  • void qsort(void elems,
  • int numElems,
  • int elemWidth,
  • int (compareFunc)
  • (void e1, void e2))

Parametric polymorphism or Overloading?
7
Example
  • Polymorphism in C
  • typedef struct
  • MusicianType type
  • union
  • Trumpeter tData
  • Flautist fData
  • / ... /
  • u
  • Musician

8
Example
  • Polymorphism in C
  • void play(Musician m)
  • switch(m-gttype)
  • case TRUMPETER
  • printf("trumpeter plays toot\n")
  • break
  • case FLAUTIST
  • printf("flautist plays tweet\n")
  • break
  • / ... /

Parametric polymorphism or Overloading?
9
Previous Example
  • Polymorphism in C
  • Assume that we have our previously defined
  • Time and PTime types.
  • The Java polymorphic type Time
  • Time t1 new Time(0,0,0)
  • Time t2 new PTime(1,2,3)
  • typedef struct
  • TimeType type
  • void timeptr
  • TimeRef

10
Previous Example
  • Java
  • Time t1 new Time(0,0,0)
  • Time t2
  • new PTime(1,2,3)
  • C
  • TimeRef t1
  • t1.type TIME
  • t1.timeptr new_Time
  • (0, 0, 0, t1.timeptr)
  • TimeRef t2
  • t2.type PTIME
  • t1.timeptr new_PTime
  • (1, 2, 3, t2.timeptr)

Note that an array of TimeRef's would be able to
hold objects of different types!
11
Previous Example
  • Java
  • t1.setTime(4,5,6)
  • C
  • switch(t1.type)
  • case(TIME)
  • ((Time)(t1.timeptr))-gt
  • setTime(4, 5, 6,
  • ((Time)(t1.timeptr)))
  • break
  • case(PTIME)
  • ((PTime)(t1.timeptr))-gt
  • setTime(4, 5, 6,
  • ((PTime)(t1.timeptr)))
  • break

Parametric polymorphism or Overloading?
12
Where else have you seen polymorphism?
13
Scheme?
  • gt (cons 'a '(1 2 3))
  • (a 1 2 3)
  • gt (cons 1 '(1 2 3))
  • (1 1 2 3)
  • gt (cons '(1 2 3) '(1 2 3))
  • ((1 2 3) 1 2 3)

14
Representation of Polymorphic Variables
  • Basic issue Size of data
  • How to write code when size of data being passed
    into function is unknown
  • Approaches
  • Boxing
  • Tagging
  • Coercion
  • Expansion
  • Tag Passing

15
Boxing
  • Scheme/List Approach
  • Put a pointer or reference to the data structure
    to be passed into a "box" so that the function
    always expects the same size item.
  • cons always expects
  • a pointer to something
  • as the first argument to
  • cons

16
Tagging
  • struct
  • int tag
  • union
  • int x
  • float y
  • Node z

Note If the data item size is less than or equal
to the size of a pointer store the data item
directly
17
Expansion
  • switch(Based on the tag)
  • case tag_type_1
  • function for tag_type_1()
  • break
  • case tag_type_2
  • function for tag_type_2()
  • break
  • ...

18
Type Passing
  • function(data/pointer tag)
  • switch(based on tag)
  • case tag1
  • code
  • break
  • case tag2
  • code
  • break
  • ...

19
Coercion
  • Function takes double
  • Before call take any type (int, float, double)
  • Conver to double
  • Call function.

20
Questions?
21
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com