Dynamic binding

3/24/99


Click here to start


Table of Contents

Dynamic binding

A simple view of the problem

Why do we care?

An obvious solution

The obvious solution can be made to work

Implementation (in C)

Using the type code

What's wrong with the simple approach?

The C++ approach: virtual functions

The function definitions look normal

Calling a virtual function

Examples

A virtual call happens when

Typical implementation

Types of virtual functions

An example

The data structure

Example code, part 1

Code, part 2

Code, part 3

Adding new shapes

Draw all the shapes

Why the virtual destructor?

What does a virtual destructor do?

Multiple abstractions

Virtual functions and type fields

Virtual functions and constructors

Another example

Sample code

How do we do it?

We can already write code

Integer expressions

Unary expressions

Binary expressions

We can generalize our sample

We can get rid of most of the variables:

Points to remember

Why aren't all C++ member functions virtual?

Summary

Homework (due Monday)

Author: Andrew Koenig

Email: ark@research.att.com

Home Page: http://www.research.att.com/info/ark