CS 2130 - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

CS 2130

Description:

Principal designer James Gosling. Designed emacs. Sidebar. Clean Shaven (by choice) ... James Gosling (emacs, Java) Peter Naur (Algol, BNF) Dennis Ritchie (C) ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 39
Provided by: ble87
Category:
Tags: gosling

less

Transcript and Presenter's Notes

Title: CS 2130


1
CS 2130
  • Presentation 31
  • Class Object Implementation
  • including Inheritence and Polymorphism

2
History
  • Simula 67
  • Designed for simulation
  • Development of class construct
  • Template for instances
  • Data and routines to manipulate data packed
    together
  • Code executed at initialization only
  • Hierarchies allowing code reuse
  • Smalltalk
  • Alan Kay Dissertation on Dynabook
  • General use computers
  • Interactive
  • Graphical interface (LOGO -- Papert)
  • Flex (based on Simula 67)
  • Dynabook concept of top sheet in focus others
    partially covered

3
History
  • Smalltalk (continued)
  • Kay joins PARC
  • Learning Research Group founded
  • Implementation of Dynabook
  • Xerox Alto Hardware
  • Smalltalk 72
  • C
  • Bjarne Soustrup - Bell Labs
  • C with Classes - 1981
  • Simula 67 Organization
  • No performance penalty
  • Used wherever C could be used
  • C 1984
  • Virtual functions, dynamic binding, function name
    operator overloading and reference types.

4
History
  • C
  • Bjarne
  • Backed by Bell
  • Easy path from C to C
  • C Initially very cheap
  • C highly available
  • Eiffel
  • Integrated use of assertions to enforce contract
  • Bertrand Meyer
  • No major backer
  • Difficult to migrate
  • Expensive
  • Compilers not available

5
History
  • Java
  • Provides much of the power and flexibility of C
  • Smaller, simpler, safer language
  • Designed for embedded consumer electronics
    devices
  • Reliability critical
  • Useful for web programming
  • Principal designer James Gosling
  • Designed emacs

6
Sidebar
  • Clean Shaven (by choice)
  • Konrad Zuse (Plankalkül)
  • John Backus (Fortran, BNF)
  • Fritz Bauer (Algol)
  • Ole-Johan Dahl (Simula)
  • Robert Kowalski (Prolog)
  • Clean Shaven (not by Choice)
  • Grace Hopper (Cobol)
  • Adele Goldberg
  • Not Clean Shaven
  • Bjarne Soustrap (C)
  • James Gosling (emacs, Java)
  • Peter Naur (Algol, BNF)
  • Dennis Ritchie (C)
  • John Kemeny (Basic, Moustache)
  • Nicklaus Wirth (Pascal, Modula)
  • Edsger Dijkstra (Concurrency)
  • John McCarthy (Lisp)
  • Brian Kernighan (C)

?
7
Question?
  • Do you have to be using an OO language to write
    OO programs?
  • 1. yes
  • 2. no

8
OO Example
  • Language Java
  • Class Time
  • Will hold a time "quantity"
  • Hours
  • Minutes
  • Seconds
  • Constructor(s)
  • Method to allow setting time
  • Calculation of time in seconds

9
  • class Time
  • private int hours, minutes, seconds
  • // Constructor 1
  • public Time()
  • setTime(0, 0, 0)
  • // Constructor 2
  • public Time(int hours, int minutes, int seconds)
  • setTime(hours, minutes, seconds)

10
  • // class Time (Continued)
  • // Modifier
  • public void setTime(int h, int m, int s)
  • this.hours h
  • this.minutes m
  • this.seconds s
  • // Accessor
  • public int elapsedSeconds()
  • return ( (this.hours 60) this.minutes )
  • 60 this.seconds
  • // class Time

11
Oh, say can you C?
  • How close can we come in C?

12
Obvious start
  • typedef struct
  • int hours
  • int minutes
  • int seconds

13
Do we put functions in here?
  • typedef struct
  • int hours
  • int minutes
  • int seconds

Do we want each instance to have a copy of the
function code?
Functions here?
14
No!
  • We'll use function pointers!

15
Function Pointers in Place
  • typedef struct timeobj
  • int hours
  • int minutes
  • int seconds
  • void (setTime) (int h, int m, int s,
  • struct timeobj this)
  • int (elapsedSeconds) (struct timeobj this)
  • Time

16
We still need to write the functions
  • void f_setTime(int h, int m, int s,
  • struct timeobj
    this)
  • this-gthours h
  • this-gtminutes m
  • this-gtseconds s
  • int f_elapsedSeconds(struct timeobj this)
  • return ( (this-gthours 60) this-gtminutes )
    60

  • this-gtseconds

17
Constructor
  • // Recall the Java version
  • Time t1 new Time()
  • Time t2 new Time(10, 20, 30)

18
Our Constructor
  • Time new_Time(int h, int m, int s)
  • Time newTime
  • newTime malloc(sizeof(newTime))
  • / Error checking omitted /
  • newTime-gtsetTime f_setTime
  • newTime-gtelapsedSeconds f_elapsedSeconds
  • newTime-gtsetTime(h, m, s, newTime)
  • return newTime
  • / Use /
  • Time t1 new_Time(0, 0, 0)
  • t1-gtsetTime(12, 34, 56, t1)

19
Inheritence
20
Java Subclass
  • class PTime extends Time
  • void printTime()
  • System.out.println(
  • "Time " hours"" minutes"" seconds)

21
Inheritance
  • Make a new structure.
  • Include in new structure the parent structure
  • typedef struct ptimeobj
  • Time super
  • void (printTime) (struct ptimeobj this)
  • PTime

22
Diagram
Time
hours
minutes
seconds
setTime
elapsedSeconds
PTime
super
printTime
23
Inheritance
  • The function
  • void f_printTime(struct ptimeobj this)
  • printf("Time d02d02d\n",
  • this-gtsuper-gthours,
  • this-gtsuper-gtminutes,
  • this-gtsuper-gtseconds)

24
Inheritance
  • The constructor
  • PTime new_PTime(int h, int m, int s)
  • PTime newPTime
  • newPTime malloc(sizeof(newPTime))
  • / Error checking omitted /
  • newPTime-gtsuper new_Time(h, m, s)
  • newPTime-gtprintTime f_printTime
  • return newPTime

25
Inheritance
  • Test main
  • int main()
  • Time t1 new_Time(0, 0, 0)
  • PTime t2 new_PTime(2, 42, 13)
  • t1-gtsetTime(12, 34, 56, t1)
  • t2-gtprintTime(t2)
  • printf("t1 elapsed time d\n",
  • t1-gtelapsedSeconds(t
    1))
  • return EXIT_SUCCESS

Time 24213 t1 elapsed time 45296
26
Polymorphism
27
What is polymorphism?
  • Webster's Dictionary poly-mor-phic able to
    assume different forms.

28
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
29
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
  • swap(anytype a, anytype b)
  • anytype t
  • t a
  • a b
  • b t

int p, q float x,y ... swap(p,q) swap(x,y) swa
p(p,y)
30
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
31
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))

32
Example
  • Polymorphism in C
  • typedef struct
  • MusicianType type
  • union
  • Trumpeter tData
  • Flautist fData
  • / ... /
  • u
  • Musician

33
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
  • / ... /

34
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

35
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!
36
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
  • super-gtsetTime(4, 5, 6,
  • ((PTime)
  • (t1.timeptr)-gtsuper))
  • break

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