You are on page 1of 144

Modularity and Object-Oriented Programming — OO

Steven Zeil November 13, 2001

1

1. OOP Philosphy 2. OOP Languages 3. Working With Inheritance 4. Some OOP Languages 5. Implementing OO

2

1. OOP Philosphy
OOP stems from work in Simula, a simulation language. Every program is a model of some part of the world. The quality of a program design is directly proportional to how faithfully it reflects the structure of the world that it models.

3

State 3. Identity 2.What is an Object? An object is characterized by 1. Behavior 4 .

x. • names: Steve.1. Identity Identity is the property of an object that distinguishes it from all others. Pˆ. y • references: this. ∗p 5 . George. that.1.

2. Author a u t h o r . 6 . State The state of an object consists of the properties of the object.1. Consider a book: s t r u c t Book { string t i t l e . ISBN i s b n . int edition . and the current values of those properties.

i n t year . Publisher publisher . }. Does this have state? 7 .

is a book object: Book cs355Text = { ” Programming Languages ” .No. however. 8 . it names properties but has no values. addisonWesley } . 1996. Here. ”0−201−59065−4” . sethi . 2.

9 . In OOP. behavior is often described in terms of messages and methods.3. in terms of visible state changes and operations on other visible objects. • A message to an object is a request for service.1. Behavior Behavior is how an object acts and reacts to operations on it.

• A method is the internal means by which the object responds. 10 .

Different objects may respond to the same message via different methods. Example: I send a message “send flowers to my grandmother” to 11 .

Different objects may respond to the same message via different methods. Example: I send a message “send flowers to my grandmother” to • a florist 11 .

Different objects may respond to the same message via different methods. Example: I send a message “send flowers to my grandmother” to • a florist • my wife 11 .

Example: I send a message “send flowers to my grandmother” to • a florist • my wife • a secretary 11 .Different objects may respond to the same message via different methods.

Different objects may respond to the same message via different methods. Example: I send a message “send flowers to my grandmother” to • a florist • my wife • a secretary • a tree 11 .

we must know what messages they will successfully respond to.In using objects. 12 . not necessarily the method of the response.

1. constant label.4. address value type funct decl funct body funct call 13 . these are familiar programming concepts already: OOPL object identity state class message method passing a message traditional PL variable . name. Are objects new? In some ways.

Subtyping 3. OOP Languages Essential characteristics of OOPL’s: 1. Inheritance 2. Dynamic Binding 14 .2.

15 . • D is called a base class of C. Inheritance A class C inherits from class D if C has all the data members and messages of D. • C may have additional data members and messages not present in D.1.2.

Given package Ovens i s type Oven i s p r i v a t e . function temp ( o : Oven ) r e t u r n s r e a l . 16 . desired : real ) . private type Oven i s record setting : real . temp : r e a l .Ada has a very pure form of inheritance. procedure setTemp ( o : i n out Oven .

17 . end Ovens .end record .

desired : real ) . use Ovens .We can then write with Ovens . MicrowaveOven inherits the data and functions of Oven. package MoreOvens i s type MicrowaveOven i s new Oven . 18 . It adds a new procedure. procedure s e t T i m e r ( o : i n out Oven . setTimer. end MoreOvens .

− − OK ( i n h ) setTimer ( o . o : Oven . − − OK bakeBread ( o ) . setTemp ( o .procedure bakeBread ( o : i n o u t Oven ) . − − i l l e g a l 19 . − − OK setTemp (m. t ). m: MicrowaveOven . v ) . −− i l l e g a l s e t T i m e r (m. − − OK bakeBread (m) . v ) . t ) .

20 . Subtyping A type C is a subtype of D if a value of type C may be used in any operation that expects a value of type D.2.2. • C is called a subclass or subtype of D. • D is called a superclass or supertype of C.

private : double s e t t i n g . void setTemp ( d e s i r e d : r e a l ) . 21 .As a general rule. }. double t e mp e r a t u r e . OOPL’s combine inheritance and subtyping. class Oven { public : double temp ( ) const .

class MicrowaveOven : public Oven { public : void s e t T i m e r ( double ) . 22 . }. private : double t i m e .C++/Java combine inheritance and subtyping.

− − OK ( sub ) 23 . − − OK ( i n h ) o .void bakeBread ( const Oven & ) . −− i l l e g a l m. Oven o . − − OK bakeBread ( o ) . o . − − OK bakeBread (m) . setTemp ( v ) . s e t T i m e r ( t ) . setTemp ( v ) . MicrowaveOven m. setTimer ( t ). − − OK m.

2.2. It is convenient for many reasons to organize the list as a pair of rings. linked at the beginning and end via a header node.1. Subtyping: Linked List As an example of subtyping in action. consider a doubly linked list. 24 .

L 3 Adams Baker Cook 25 .

}. }. Node *prev. Node* first. But sometimes a Node must point to a Header rather than another Node. Node* last. 26 .We might first try: struct Node { string data. struct Header { int listSize. Node* next.

a Node∗ or Header∗ could be substituted anywhere a DLLBase∗ was expected 27 . • Suppose that Node and Header were each subtypes of another type. DLLBase.An elegant solution is offered by subtyping. • Then by the subtyping rule.

s t r u c t Header : public DLLBase { int listSize . s t r u c t Node : public DLLBase { s t r i n g data . }. DLLBase ∗ n e x t . 28 . DLLBase ∗ prev .s t r u c t DLLBase { } . DLLBase ∗ l a s t .

29 . }.DLLBase ∗ f i r s t .

Still not ideal. while ( p ! = header ) { process ( p−>data ) . } fails because a DLLBase does not have next or data fields. 30 . because code like DLLBase ∗ p = header−> f i r s t . p = p−>n e x t .

p = ( ( Node ∗ ) p)−>n e x t . } 31 .We can rewrite using casts: DLLBase ∗ p = header−> f i r s t . while ( p ! = header ) { process ( ( ( Node ∗ ) p)−>data ) .

L 3 Adams Baker Cook 32 .or we can recognize that everything in this list has two pointers.

}. s t r u c t Node : public DLLBase { s t r i n g data . DLLBase ∗ f o r w a r d .s t r u c t DLLBase { DLLBase ∗ backward . }. 33 .

}. 34 .s t r u c t Header : public DLLBase { int listSize .

DLLBase ∗ p = header−>f o r w a r d . while ( p ! = header ) { process ( ( ( Node ∗ ) p)−>data ) . 35 . } Although we still have a type cast. p = p−>f o r w a r d . this is relatively insignificant.

I will have something like: 36 .If I package up my DLList as an ADT.

public: typedef DLLBase* Position..} Position next(Position p) const {return p->next.. find .} }.. erase..} Position end() const {return header. insert. StringList(). ˜StringList(). */ string& at(Position p) {return ((Node*)p)->data. 37 .class StringList { private: Header* header.} /* .} Position prev(Position p) const {return p->prev. Position begin() const {return header->next().

38 .

when is the decision made as to what code will be executed for this call to foo? 39 .2.3. Given the following: a = foo ( b ) . Dynamic Binding Consider the binding of function body (method) to a function call.

the decision is made at compile-time.1. etc.3. same code will be invoked for foo each time. PASCAL.2. Static Binding a = foo ( b ) . compiled imperative language (FORTRAN.). C. In a traditional. • Decision is immutable • If this statement is inside a loop. • Compile-time binding — low execution-time 40 .

41 .overhead.

BASIC. the decision is made at run-time. different code may be invoked for foo each time. In a traditionally interpreted language (LISP. Unconstrained Dynamic Binding a = foo ( b ) . • Decision is often mutable • If this statement is inside a loop.2. • Run-time binding — high execution-time overhead.3. 42 . etc).2.

3. OOP Dynamic Binding OOPLs typically feature an “intermediate” form of dynamic binding in which the choice of method is made from a relatively small list of options.3. 43 .2.

OO Dynamic Binding • list is determined at compile time 44 .

OO Dynamic Binding • list is determined at compile time • final choice made at run-time 44 .

OO Dynamic Binding • list is determined at compile time • final choice made at run-time • options are organized according to the inheritance hierarchy 44 .

OO Dynamic Binding • list is determined at compile time • final choice made at run-time • options are organized according to the inheritance hierarchy • determined by the actual type of the object whose member function has been selected 44 .

45 .• In SmallTalk & Java. we can choose between compiletime and dynamic binding. all function calls are resolved by dynamic binding. • In C++.

2. 46 .3.4. Dynamic Binding in C++ • A non-inherited function member is subject to dynamic binding if its declaration is preceded by the word “virtual”.

• Using the word “virtual” in subclasses is optional (but recommended). 47 .• An inherited function member is subject to dynamic binding if that member in the base class is subject to dynamic binding.

• Using the word “virtual” in subclasses is optional (but recommended).• An inherited function member is subject to dynamic binding if that member in the base class is subject to dynamic binding. calls to that member might not be subject to dynamic binding. • Even if a member function is virtual. 47 .

where x is a pointer. where x is an object. • x->foo(). 48 . is bound at compile time (static) • x. • x. where x is a reference to an object. is bound at run-time (dynamic).Dynamically Bound Calls Let foo be a virtual function member. is bound at run-time (dynamic).foo().foo().

} }. } S t r i n g name ( ) { r e t u r n ” Animal ” . class H e r b i v o r e : public Animal { public : v i r t u a l S t r i n g eats ( ) { return ” p l a n t s S t r i n g name ( ) { r e t u r n ” H e r b i v o r e ” .class Animal { public : v i r t u a l S t r i n g eats ( ) { return ”???” . 49 . } }.

class Ruminants : public H e r b i v o r e { public : v i r t u a l S t r i n g e a t s ( ) { r e t u r n ” grass S t r i n g name ( ) { r e t u r n ” Ruminant ” . 50 . } }. } }. class C a r n i v o r e : public Animal { public : v i r t u a l S t r i n g e a t s ( ) { r e t u r n ” meat ” S t r i n g name ( ) { r e t u r n ” C a r n i v o r e ” .

void show ( S t r i n g s1 . } 51 . S t r i n g s2 ) { c o u t << s1 << ” ” << s2 << e n d l .

∗ pa . 52 . name ( ) . show ( pa−>name ( ) . Ruminant r . par = & r .Animal a . ∗ ph . par−>e a t s ( ) ) . name ( ) . show ( ph−>name ( ) . e a t s ( ) ) . show ( h . ph−>e a t s ( ) ) . ∗ par . pa−>e a t s ( ) ) . e a t s ( ) ) . a . show ( par−>name ( ) . show ( pah−>name ( ) . pah−>e a t s ( ) ) . show ( a . H e r b i v o r e h . pah = & h . ph = & h . pa = & a . h . ∗ pah .

∗ pah . ph = & h . Ruminant r .Animal a . a . H e r b i v o r e h . e a t s ( ) ) . ∗ ph . show ( a . ∗ par . 53 . par = & r . ∗ pa . name ( ) . pah = & h . pa = & a .

Animal a . pa = & a . / / Animal ??? 53 . pah = & h . a . name ( ) . ∗ par . ∗ ph . e a t s ( ) ) . show ( a . ∗ pa . H e r b i v o r e h . Ruminant r . ∗ pah . par = & r . ph = & h .

par = & r . show ( a . a .Animal a . Ruminant r . pa = & a . ∗ pah . name ( ) . H e r b i v o r e h . pah = & h . ph = & h . 53 . ∗ ph . pa−>e a t s ( ) ) . e a t s ( ) ) . ∗ pa . ∗ par . / / Animal ??? show ( pa−>name ( ) .

pa = & a . / / Animal ??? show ( pa−>name ( ) . H e r b i v o r e h . / / Animal ??? 53 . ph = & h . ∗ pah . ∗ pa . pah = & h . name ( ) . a . ∗ ph . show ( a . ∗ par . e a t s ( ) ) . Ruminant r .Animal a . pa−>e a t s ( ) ) . par = & r .

pah = & h . ph = & h . name ( ) . par = & r . 54 . H e r b i v o r e h . e a t s ( ) ) . ∗ par .Animal a . show ( h . ∗ pa . ∗ ph . ∗ pah . h . pa = & a . Ruminant r .

e a t s ( ) ) . name ( ) .Animal a . show ( h . / / Herbivore plants 54 . Ruminant r . h . ∗ pa . pa = & a . ∗ par . ∗ pah . H e r b i v o r e h . ph = & h . pah = & h . ∗ ph . par = & r .

∗ ph . pah = & h .Animal a . name ( ) . par = & r . ph−>e a t s ( ) ) . e a t s ( ) ) . Ruminant r . ph = & h . pa = & a . ∗ pah . show ( h . H e r b i v o r e h . ∗ pa . h . / / Herbivore plants show ( ph−>name ( ) . 54 . ∗ par .

/ / Herbivore plants show ( ph−>name ( ) . e a t s ( ) ) . ∗ ph .Animal a . H e r b i v o r e h . name ( ) . / / Herbivore plants 54 . pa = & a . ∗ pa . h . ∗ pah . Ruminant r . pah = & h . ph−>e a t s ( ) ) . show ( h . ph = & h . par = & r . ∗ par .

∗ pa . show ( pah−>name ( ) . ph = & h . ∗ par . ∗ pah .Animal a . ∗ ph . pah = & h . pa = & a . pah−>e a t s ( ) ) . H e r b i v o r e h . 55 . par = & r . Ruminant r .

Ruminant r . ∗ pa . pa = & a . ∗ pah . ph = & h . ∗ ph . ∗ par . H e r b i v o r e h . / / Animal plants 55 . pah = & h . pah−>e a t s ( ) ) .Animal a . par = & r . show ( pah−>name ( ) .

pah−>e a t s ( ) ) .Animal a . par = & r . 55 . / / Animal plants show ( par−>name ( ) . ph = & h . ∗ pah . show ( pah−>name ( ) . pah = & h . Ruminant r . par−>e a t s ( ) ) . ∗ ph . ∗ pa . pa = & a . ∗ par . H e r b i v o r e h .

Animal a . par = & r . pah = & h . plants par−>e a t s ( ) ) . pa = & a . / / Animal show ( par−>name ( ) . H e r b i v o r e h . / / Animal pah−>e a t s ( ) ) . ∗ par . Ruminant r . ∗ pah . show ( pah−>name ( ) . ∗ pa . ph = & h . ∗ ph . grass 55 .

56 .3. • Every Shape has a center point and a bounding rectangle. Working With Inheritance Example: Shapes • A Picture contains a list of Shapes and information on the size of the picture.

57 . y = min ( u l . x = min ( u l . some utility classes: struct Point { double x . r . u l . void merge ( const Rect & r ) { u l . l r . y .First. x . }. s t r u c t Rect { Point ul . r . u l . y . y ) . x ) . u l .

x . r . x = max ( l r . l r . y .l r . l r . l r . y = max ( l r . 58 . } }. r . y ) . x ) .

v i r t u a l ˜ Shape ( ) . v i r t u a l const Rect bound ( ) const . v i r t u a l void draw ( ) const . v i r t u a l const P o i n t c e n t e r ( ) const . v i r t u a l void zoom ( double f a c t o r ) . 59 . }.class Shape { public : Shape ( ) .

picture 60 .

ShapeList ∗ n e x t .s t r u c t ShapeList { Shape ∗ shape . 61 . }.

void c l e a r ( ) . virtual ˜ Picture ( ) .class P i c t u r e { public : Picture ( ) . void draw ( ) const . void add ( const Shape & ) . Rect bound ( ) const . 62 .

}.void zoom ( double f a c t o r ) . private : ShapeList ∗ shapes . 63 .

double r ) . ˜ Circle ( ) . void draw ( ) const . Rect bound ( ) const . 64 .class C i r c l e : public Shape { Point center . public : C i r c l e ( P o i n t c e n t . C i r c l e ( const C i r c l e & ) . double r a d i u s . void zoom ( double f a c t o r ) .

65 .}.

void draw ( ) const . void zoom ( double f a c t o r ) . }. public : L i n e ( P o i n t p1 . ˜ Line ( ) . 66 . Rect bound ( ) const . l r . double p2 ) .class L i n e : public Shape { Point ul . L i n e ( const L i n e & ) .

67 .Drawing a picture: void P i c t u r e : : draw ( ) const { Shape ∗ s = shapes . } } Relies on dynamic binding of draw(). while ( s ! = 0 ) { s−>shape−>draw ( ) . s = s−>n e x t .

68 . case c i r c l e T a g : d r a w C i r c l e ( ∗ s ) . while ( s ! = 0 ) { switch ( s−>t a g ) { case l i n e T a g : drawLine ( ∗ s ) . break .Without dynamic binding. this would have been written as void P i c t u r e : : draw ( ) const { Shape ∗ s = shapes . break .

. .. } s = s−>n e x t . } } 69 .

70 .Moving to OOP A key marker in OOP style is the replacement of • unions (variant records) • multi-way branches by dynamic binding over an inheritance hierarchy.

l r . ul . b . x = center . l r . y = center . y − b . y = center . radius .Let’s look at what’s involved in computing the size of a picture: Rect C i r c l e : : bound ( ) { Rect b . x − b . ul . y + return b . radius . x + b . } radius . x = center . radius . 71 .

} 72 . return b . lr = lr . b. ul = ul . b .Rect L i n e : : bound ( ) { Rect b .

if (s != 0) { Rect r = s−>shape−>bound ( ) . s = s−>n e x t . merge ( s−>shape−>bound ( ) ) . s = s−>n e x t .Rect P i c t u r e : : bound ( ) const { Shape ∗ s = shapes . } } 73 . while ( s ! = 0 ) { r .

74 .Relies on dynamic binding of bound().

etc. 75 . design new Ellipse type.. • .g.What’s involved in adding a new shape (e.. ellipse)? • In traditional languages.. • Must supply ellipse-specific code for drawEllipse(). boundEllipse().

• . • add a new case to that code 76 ... • Find all code everywhere (Picture plus applications) that switches based on tags.

. ellipse)? • In OOPL. • No changes to Picture required 77 .What’s involved in adding a new shape (e. bound(). etc. design new Ellipse class. • Subclass of Shape • Must supply ellipse-specific code for draw().g.

Some OOP Languages • OOP in C++ • OOP in Java • OOP in Smalltalk • OOP in Modula 3 78 .4.

• C++ is a hybrid language.4. • Inheritance hierarchies tend to be small and special-purpose.1. • Not all types are classes. • Not all class member functions dynamically bound. OOP in C++ We’ve seen the basics already. 79 .

4. v i r t u a l void draw ( ) const . class Shape { public : Shape ( ) . v i r t u a l ˜ Shape ( ) .1. Abstract Classes Sometimes we present class interfaces that are intended only as interfaces — no implementation is expected. 80 . v i r t u a l void zoom ( double f a c t o r ) .1.

81 . }. v i r t u a l const Rect bound ( ) const .v i r t u a l const P o i n t c e n t e r ( ) const .

we mark them as abstract.• Can’t really provide meaningful code for draw(). 82 . • No bodies provided for abstract functions • Abstract classes cannot be instantiated as an actual object. • The implementation is done in a subclass. etc. • Rather than provide bogus code for the operations. bound().

v i r t u a l const Rect bound ( ) const = 0 . }. v i r t u a l void zoom ( double f a c t o r ) = 0 v i r t u a l const P o i n t c e n t e r ( ) const = 0 . 83 .class Shape { public : Shape ( ) . v i r t u a l ˜ Shape ( ) . v i r t u a l void draw ( ) const = 0 .

4. but purer than C++. • All classes organized into a single inheritance tree • Garbage collection 84 . OOP in Java Another hybrid.2. • All class member functions are dynamically bound. • Not all types are classes.

• C++-like syntax 85 .

x = min ( u l .Java implementation of Shapes is very similar to C++: class P o i n t { double x . u l . u l . y . void merge ( Rect r ) { u l . u l . 86 . } class Rect { Point ul . y ) . x . r . r . x ) . y . y = min ( u l . l r .

r .l r . } } 87 . l r . x = max ( l r . y = max ( l r . y ) . r . l r . x . x ) . y . l r .

a b s t r a c t Rect bound ( ) const . abstract Point center ( ) . } Establishes the common interface for all shapes.abstract class Shape { a b s t r a c t void draw ( ) . 88 . a b s t r a c t void zoom ( double f a c t o r ) .

no need for explicit pointers. 89 . Since all class objects are assigned by reference. }. ShapeList n e x t .class ShapeList { Shape shape .

class P i c t u r e { p r i v a t e ShapeList shapes ; Picture ( ) {. . . }

void c l e a r ( ) { . . . } void add ( const Shape s ) { ShapeList newNode = new ShapeList ; newNode . shape = s ; newNode . n e x t = shapes ; shapes = newNode ; 90

} Rect bound ( ) { . . . } void draw ( ) { . . . } void zoom ( double f a c t o r ) { . . . } };

91

class C i r c l e extends Shape { private Point center ; p r i v a t e double r a d i u s ; C i r c l e ( P o i n t c e n t , double r ) { . . . } void draw ( ) { . . . } void zoom ( double f a c t o r ) { . . . } Rect bound ( ) { . . . } };

92

Drawing a picture: class P i c t u r e { . . void draw ( ) { { Shape s = shapes . . next . draw ( ) . } } 93 . s = s . shape . while ( s ! = n u l l ) { s .

} 94 .

• interpreted (usually) • Everything is an object.4.3. In many ways. 95 . (Even classes are objects!) • All function calls are resolved dynamically. OOP in Smalltalk Smalltalk is both a language and a GUI development environment. this is the language that kicked off the OO boom.

• All classes are arranged into a single inheritance tree: 96 .

O bject Class Magnitude Collection Char Number Point Set Keyed Collection Integer Float Fraction 97 .

AShape s u b c l a s s : # Rectangle instanceVariableNames : ’ t h e H e i g h t theWidth ’ classVariableNames : setHeight : anInteger ” set the height of a rectangle ” theHeight : = anInteger setWidth : anInteger ” set the width of a rectangle ” 98 .

theWidth anInteger 99 .

height ” r e t u r n the height of a rectangle ” ˆ theHeight width ” r e t u r n the width of a rectangle ” ˆ theWidth 100 .

draw ” draw t h e r e c t a n g l e ” | aLine u p p e r L e f t C o r n e r | aLine : = L i n e new . upperLeftCorner : = t h e C e n t e r x − ( theWidth / 2 ) @ ( theCenter y − ( theHeight / 2 ) ) . 101 . theCenter x means “send the “x” message to the theCenter object”. ARectangle inherits a data member theCenter from AShape.

• Note the lack of ”syntactic sugar” • one seldom really looks at ”listings” of an entire class • Variables are weakly typed. • Automatic garbage collection 102 .

A SmallTalk Browser Graphics Array insertion Collections Bag removal Numerics FIFOQueue testing System Set printing add: x ”Adds x to the end of the queue” size := size + 1. dataArray at: size put: x. add: addAll: 103 .

104 .

• kept Pascal/Modula 2 style syntax and emphasis on simplicity • adopted C-like (structural equivalance) type system • Modularity separate from type declaration • garbage collection by default on all pointers 105 .4. OOP in Modula 3 In the Pascal/Modula 2 tradition.4.

• programmer can exempt selected classes from garbage collection (for efficiency) • avail of garbage collection simplifies language and code • much less emphasis on constructors & destructors 106 .

TYPE T < : REFANY.INTERFACE Stack . PROCEDURE Push (VAR s : T . • This is a module Stack that declares a class Stack.T 107 . PROCEDURE Pop (VAR s : T ) : REAL. x : REAL ) . PROCEDURE Create ( ) : T . END Stack .

108 .• <: is the ”inherits from” operator • The code of the procedure bodies is very Pascal-like.

5. Implementing OO
How are inheritance, dynamic dispatching, etc., implemented?

109

5.1. Implementing and Subtyping

Inheritance

Inheritance of data members is achieved by treating all new members as extensions of the base class:
s t r u c t DLLBase { DLLBase ∗ backward ; DLLBase ∗ f o r w a r d ; }; 110

s t r u c t Node : public DLLBase { s t r i n g data ; };

111

Extension Inherited members occur at the same byte offset as in the base class: Node 0 backward DLLBase 4 forward 0 backward 8 4 forward data p->forward can translate the same whether p points to a DLLBase or a Node. 112 .

113 .Assignment & Subtyping This implementation explains why. in languages with copy semantics assignments. we can do superObj = subObj . but not subObj = superObj .

long i d . } 114 . }.class Person { public : s t r i n g name . class Student : public Person { public : s t r i n g school .

jones 111110 smith 456782 ODU 115 .

jones 111110 smith 456782 ODU jones = smith . 115 .

jones 111110 smith 456782 ODU jones = smith . smith 456782 smith 456782 ODU 115 .

jones 111110

smith 456782 ODU

116

jones 111110

smith 456782 ODU

smith = jones ;

116

jones 111110

smith 456782 ODU

smith = jones ;
jones 111110 jones 111110 x!@z-

116

2. Implementing Dynamic Binding • Single Dispatching • VTables 117 .5.

including C++. offer a single dispatch model of message passing: the dynamic binding is resolved according to the type of the single object to which the message was sent (“dispatched”). 118 .Single Dispatching Almost all OO languages.

.. simple implementation 119 . • But it leads to a fast.Single Dispatching (cont.) • There are times when this is inappropriate.) • In C++.foo(. this is the object on the left in a call: obj.

start or end of the object • Each virtual function in a class is assigned a unique. consecutive index number. 120 . • a pointer to a VTable for it’s class • this member is always at a predictable location (e.g..• Each object with 1 or more virtual functions has a hidden data member.

121 .• (∗VTable)[i] is the address of the class’s method for the i’th virtual function.

virtual void baz(). class B: public A { public: B(). }. bar(). A ∗ b = ( rand ( ) % 2 ) ? new A : new B . }. and 2. b−>f o o ( ) .class A { public: A(). virtual void foo(). respectively. foo(). 1. virtual void bar(). and baz() are assigned indices 0. 122 . virtual void foo().

a1 A::foo() A:bar() b1 a2 B::foo() A:bar() B:baz() The call b->foo() is translated as ∗ ( b−>VTABLE [ 0 ] ) ( ) . 123 .