CS300: Object Oriented Analysis and Design
Run Time Polymorphism
Polymorphism
Polymorphism in C++
Compile Time
Function overloading
Operator overloading
Runtime
Virtual Function
The provision of a single interface to entities of different types.
Methods with same name but different implementations.
Operators have different implementations depending on their arguments
A member function that you expect to be redefined in derived classes.
Introduction
Encapsulation
Access control
Inheritance
Virtual function
Creates new data types
Separates the interface from the implementation
An object as its own type or its base type
Paradigm shift in programming
Better C
Object Based
Object oriented
Polymorphism
Polymorphism
Early
Binding occurs at compile time
Early binding polymorphism
Process of overloading members
Late
Binding occurs at runtime
Late binding polymorphism
The code to implement the method is chosen at runtime
Appropriate code chosen sending a message to
the object …. Not to the pointer to the object
Implemented through virtual functions
1 February2006
5
Copyright 2006 Oxford Consulting, Ltd
Pointers to Derived Types
B
D
A base class pointer can point to any derived classes object
Demonstration
Virtual Function
Binding
Types of Bindings
Early
Late
Before the program is run (by the compiler and linker)
Binding occurs at runtime, based on the type of the object
Dynamic binding
or
Runtime binding
Virtual functions
Polymorphism
Virtual Functions
A virtual function must be declared in a parent class
syntax
virtual function
virtual returnType functionName ( argsi ) { function body ;}
pure virtual function
virtual returnType functionName ( argsi ) = 0;
1 February2006
10
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions
Declaration
A function name is preceded by the keyword virtual
A function is virtual…..
declared virtual
Any or all class member functions (except constructors)
can be declared virtual
1 February2006
11
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions
Implementation
class unless declared to be a pure virtual function
implementation
If the re-declaration does not match exactly…...
The function not considered virtual for that class
1 February2006
12
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions
Such a capability permits multiple functions to be called through a
common interface.
Can be overridden by explicit qualification with the scope operator.
1 February2006
13
Copyright 2006 Oxford Consulting, Ltd
Gives Uniform Function
Call Interface
Base
Derived1
Derived2
Derived3
……..
Derivedn
Public Interface
Polymorphism
Virtual Functions
When function in a class is declared virtual
Keyword virtual tells compiler
Compiler responds by creating
1 February2006
14
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions
The compiler created table is called the vtable (vtbl)
Contains pointers to all functions declared virtual within the class
and derived classes.
Each class gets its own vtable
A data member called the vpointer (vPtr)
Usually placed as the first element in object in memory.
Initialized to the starting address of the vtable.
The function call through a base class pointer
Indexes into the vtable calls the function located at the address.
1 February2006
15
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
1 February2006
16
Copyright 2006 Oxford Consulting, Ltd
class A
{
public:
int i;
virtual void f ( );
virtual void g( );
};
vptr
i
&f ( )
&g ( )
vtable[0]
vtable[1]
class A
class A
vtable
Polymorphism
1 February2006
17
Copyright 2006 Oxford Consulting, Ltd
class A
{
public:
int i;
virtual void f ( );
};
class B : public A
{
public:
virtual void f ( ); // override f( )
virtual void g ( ); // define g ()
};
class A
vtable
&f ( )
vtable[0]
&f ( )
&g ( )
vtable[0]
vtable[1]
class B
vtable
Polymorphism
Virtual Functions - vtable
1 February2006
18
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions - Invocation
A virtual function is invoked through a public base class pointer or reference.
Runtime Binding
Compile Time Binding
1 February2006
19
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions - Access Protection
The access level of a virtual function is determined by
1 February2006
20
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
A
B : public A
C : public B
D : public C
E : public D
1 February2006
21
Copyright 2006 Oxford Consulting, Ltd
Declare a and public virtual function f() public virtual function f() in B
protected virtual function f() in C
private virtual function f() in D
Write:
1. A* a = new B;
a-> f(); // f() accessible through *a (as a *B - *a in public area)
2. a = new C;
a-> f(); // f() accessible through *a (as a *C - a* in public area)
3. C* c = new D;
c-> f(); // f() not accessible through *c (as a *D - c* in protected area)
Polymorphism
1 February2006
22
Copyright 2006 Oxford Consulting, Ltd
Virtual Destructors
When a base class pointer is used to refer to a derived class object and the object is deleted…..
Only the base class destructor will be invoked leaving behind the derived class parts of the object.
Polymorphism
Virtual Destructors
syntax
virtual ~ class ClassName ( ) { destructor body }
Specifying a destructor as virtual ensures all appropriate destructors
are invoked.
Rule of thumb…..If a class is abstract then declare the destructor as
virtual.
Don’t declare the destructor if there are no other virtual functions.
1 February2006
23
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Destructors
Invocation order….
…...Invoked in turn in normal fashion
1 February2006
24
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Polymorphism and Object Slicing
One must exercise caution when treating objects polymorphically
There is a distinct difference between passing objects by value and by reference.
When a derived class object is passed by value to a function expecting a base class value….
The derived class portion is sliced off.
1 February2006
25
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Pure Virtual Functions
….A virtual function must be defined when it is declared
may still be supplied
1 February2006
26
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Pure Virtual Functions
When a function is declared pure…..
1 February2006
27
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Pure Virtual Functions
Restrictions
A class with one or more pure virtual functions.
1. Can only be used as a base class
2. Cannot have instances
3. Cannot be used as
4. Can be used as a
A class cannot define a pure virtual destructor
1 February2006
28
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Pure Virtual Definitions
There may be occasions when it’s desirable to share code with
derived classes but not duplicate in each class.
Can prevent base class instantiation yet provide a definition for a pure
virtual function.
syntax
virtual returnType functionName ( argsi ) = 0 { function body }
access
BaseClassName :: functionName ( );
1 February2006
29
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions
Rules for Virtual Functions
1. Virtual functions called from within a constructor use the local
version.
2. The first class in a derivation hierarchy that declares a virtual function it must provide a definition or it must declare the virtual function to be pure
3. If a definition is provided, the definition serves as the default instance in subsequent derivations
4. If pure, a subsequently derived class must provide a definition - to have instances or inherit the pure virtual function - have no instances
1 February2006
30
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Functions
1 February2006
31
Copyright 2006 Oxford Consulting, Ltd
vf11( )
vf12( )
must define vf11( ) and vf12( )
vf12( )
can have instances
pvf13( )
no instances
no instances
pvf13( ) =
defines pvf13( )
can have instances
Class 1
Class 2
Class 3
Class 4
Class 5
Polymorphism
Virtual Functions - Access Level
The access level of a virtual function is…..
1 February2006
32
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Base Classes
Parent classes may have a common base class
1 February2006
33
Copyright 2006 Oxford Consulting, Ltd
Fruit
Peach
Fruit
Plum
Peach
Nectarine
Stem
Stem
Plum
Polymorphism
Virtual Base Classes
Problem:
Plum::stem
Peach::stem
1 February2006
34
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Base Classes
Solution:
Declare Fruit as a virtual base class
Result:
Only a single copy of the base class in the derivation hierarchy.
Only a single copy of all inherited data members.
Subsequent derivations point to shared members.
1 February2006
35
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Base Classes - Specification
Syntax
class DerivedClass : virtual accessSpec BaseClass
DerivedClass - The class being derived
BaseClass - The parent class
Specification - Specify base class member access
public
protected
private
The keyword virtual identifies BaseClass as a virtual base class of
DerivedClass.
1 February2006
36
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Base Classes - Implementation
1 February2006
37
Copyright 2006 Oxford Consulting, Ltd
B Data Members
A Data Members
A Data Members
B Data Members
Virtual Derivation
Non-Virtual Derivation
Polymorphism
Virtual Base Classes - Access Protection
When there are multiple paths from a common root …
….the most public path dominates.
1 February2006
38
Copyright 2006 Oxford Consulting, Ltd
Fruit
Plum
Peach
Nectarine
virtual public
virtual private
Polymorphism
Virtual Base Classes - Initialization
A virtual base class is initialized by the most derived class.
Initialization Order:
1. Constructors for any virtual base class(es).
2. Constructors for any non-virtual base class.
3. The most derived class must provide initialization values.
1 February2006
39
Copyright 2006 Oxford Consulting, Ltd
Polymorphism
Virtual Base Classes - Initialization
Specify class E…
class E : public D, public C, public virtual F
1 February2006
40
Copyright 2006 Oxford Consulting, Ltd
A
virtual
virtual
B
C
D
E
F
virtual
virtual base class
How C++ implements late binding
Storing type information
An Example
Instrument; play(), what(), adjust()
Wind
Percussion
Stringed
Brass
Woodwind
Demonstration
Visualizing virtual functions
Visualizing virtual functions
The Virtual Attribute Is Inherited
When a virtual function is inherited, its virtual nature is also inherited.
Base class
Virtual function
Derived class 1
Virtual function
Derived class 2
Virtual function
No matter how many times a virtual function is inherited, it remains virtual
Inheritance and the VTABLE
RTTI: Run Time Type Identification
Hierarchical Virtual Functions
Base class
Virtual function
Derived class 1
Virtual function
Derived class 2
Virtual function
Case 1
Base class
Virtual function
Derived class 1
Virtual function
Derived class 2
Case 2
Base class
Virtual function
Derived class 1
Virtual function
Derived class 2
Case 3
Overheads of virtual functions
https://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf