Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is...

34
Copyright 2006 Oxford Consulting, Ltd 1 February2006 - 1 - Polymorphism Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same general type of action….. Accomplished in different ways By different types of objects The underlying software system Decides how to achieve the action

Transcript of Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is...

Page 1: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 1 -

PolymorphismPolymorphism

Polymorphism

Polymorphism is a major strength of an object centered paradigm

Same general type of action…..

Accomplished in different ways

By different types of objects

The underlying software systemDecides how to achieve the action

Page 2: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 2 -

PolymorphismPolymorphism

Polymorphism

Key issue…..

When to implement the actionCompile time

Early Binding

Allows greater execution speed

Achieved through optimized code

Run time

Late Binding

Allows for greater flexibility

Opportunity for abstraction

Page 3: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 3 -

PolymorphismPolymorphism

Polymorphism and C++

Early

Binding occurs at compile timeEarly binding polymorphism

Process of overloading members

Late

Binding occurs at runtimeLate binding polymorphism

The code to implement the method is chosen at runtime

Appropriate code chosen sending a message tothe object …. Not to the pointer to the object

Implemented through virtual functions

Page 4: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 4 -

PolymorphismPolymorphism

Virtual Functions

A virtual function must be declared in a parent class

syntaxvirtual function

virtual returnType functionName ( argsi ) { function body ;}

pure virtual function

virtual returnType functionName ( argsi ) = 0;

Page 5: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 5 -

PolymorphismPolymorphism

Virtual Functions

DeclarationA function name is preceded by the keyword virtual

Function name can only be used once in the parent class Cannot overload virtual functions Only class member functions can be declared virtual

A function is virtual….. If it is declared virtual

There is a base class function with the same signature declared virtual

Any or all class member functions (except constructors) can be declared virtual

Page 6: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 6 -

PolymorphismPolymorphism

Virtual Functions

Implementation

The body of the virtual function must be supplied in the parent

class unless declared to be a pure virtual function

A derived class can override the definition by providing its own

implementationIf the re-declaration does not match

exactly…...

The function not considered virtual for that class

A virtual function still permitted in a subsequently derived class

Page 7: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 7 -

PolymorphismPolymorphism

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.

Gives Uniform FunctionCall Interface

Base

Derived1Derived2Derived3

……..Derivedn

Public Interface

Page 8: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 8 -

PolymorphismPolymorphism

Virtual Functions

When function in a class is declared virtual

Keyword virtual tells compiler Don’t perform early binding Install mechanisms to perform late binding

Compiler responds by creating Table of function pointers Installing a data member to the class to point to the table

Page 9: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 9 -

PolymorphismPolymorphism

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.

Page 10: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 10 -

PolymorphismPolymorphism

class A

{

public:

int i;

virtual void f ( );

virtual void g( );

};

vptr

i

&f ( )

&g ( )

vtable[0]

vtable[1]

class A

class Avtable

Page 11: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 11 -

PolymorphismPolymorphism

class A

{

public:

int i;

virtual void f ( );

};

class B : public A

{

public:

virtual void f ( ); // override f( )

virtual void g ( ); // define g ()

};

class Avtable

&f ( )vtable[0] &f ( )

&g ( )

vtable[0]

vtable[1]

class Bvtable

Page 12: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 12 -

PolymorphismPolymorphism

Virtual Functions - vtable

Contains pointers to all virtual functions.

Each class gets its own vtable.

Abstract classes have no vtable.

Vtable size is proportional to the number of virtual functions.

Page 13: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 13 -

PolymorphismPolymorphism

Virtual Functions - Invocation

A virtual function is invoked through a public base class pointer or

reference.Runtime Binding Typically polymorphic binding is done dynamically at

runtime Virtual functions are not inlined

Compile Time Binding Occasionally have compile time polymorphic binding Invoked through an object of the class type Invoked using the scope operator Invoked through a constructor or destructor

Page 14: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 14 -

PolymorphismPolymorphism

Virtual Functions - Access Protection

The access level of a virtual function is determined by

Access level in class of the pointer through which it’s invoked

Not by the class in which it’s defined.

Page 15: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 15 -

PolymorphismPolymorphism

A

B : public A

C : public B

D : public C

E : public D

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)

Page 16: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 16 -

PolymorphismPolymorphism

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.

Page 17: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 17 -

PolymorphismPolymorphism

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.

Page 18: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 18 -

PolymorphismPolymorphism

Virtual Destructors

Invocation order….

The derived type destructor

The destructor for each base class…..…...Invoked in turn in normal fashion

Page 19: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 19 -

PolymorphismPolymorphism

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.

Page 20: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 20 -

PolymorphismPolymorphism

Pure Virtual Functions

….A virtual function must be defined when it is declared Abstract base class may be defined that is not intended to

be instantiated

If virtual function is declared pure …. an implementation may still be supplied

Derived class may use implementation

Page 21: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 21 -

PolymorphismPolymorphism

Pure Virtual Functions

When a function is declared pure…..

There is no address to put into the vtable

The 0 keys the compiler that no instances of

this class can be created

Page 22: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 22 -

PolymorphismPolymorphism

Pure Virtual FunctionsRestrictions

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 An argument type Return type Type for explicit conversion

4. Can be used as a Pointer Reference type

A class cannot define a pure virtual destructor

Page 23: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 23 -

PolymorphismPolymorphism

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.

syntaxvirtual returnType functionName ( argsi ) = 0 { function body }

access

BaseClassName :: functionName ( );

Page 24: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 24 -

PolymorphismPolymorphism

Virtual FunctionsRules 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

Page 25: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 25 -

PolymorphismPolymorphism

Virtual Functions vf11( )vf12( ) must define vf11( ) and vf12( )

vf12( ) can have instances

pvf13( ) no instances

no instances

pvf13( ) = defines pvf13( ) can have instances

Class 1Class 1

Class 2Class 2

Class 3Class 3

Class 4Class 4

Class 5Class 5

Page 26: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 26 -

PolymorphismPolymorphism

Virtual Functions - Access Level

The access level of a virtual function is…..

Specified in the class where it is defined

Not by initial definition

Page 27: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 27 -

PolymorphismPolymorphism

Virtual Base Classes

Parent classes may have a common base class

Fruit

Peach

Fruit

PlumPeach

Nectarine

Stem Stem

Plum

Page 28: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 28 -

PolymorphismPolymorphism

Virtual Base Classes

Problem:

Fruit has a stem data member

Peach and plum each inherit a stem member from Fruit

Nectarine inherits a stem member from each

Could resolve using the scope operator

Plum::stem

Peach::stem

Page 29: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 29 -

PolymorphismPolymorphism

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.

Page 30: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 30 -

PolymorphismPolymorphism

Virtual Base Classes - Specification

Syntax

class DerivedClass : virtual accessSpec BaseClassDerivedClass - The class being derivedBaseClass - The parent classSpecification - Specify base class member access

publicprotectedprivate

The keyword virtual identifies BaseClass as a virtual base class of DerivedClass.

Page 31: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 31 -

PolymorphismPolymorphism

Virtual Base Classes - Implementation

B Data Members

A Data Members

A Data Members

B Data Members

Virtual Derivation

Non-Virtual Derivation

Page 32: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 32 -

PolymorphismPolymorphism

Virtual Base Classes - Access Protection

When there are multiple paths from a common root …

….the most public path dominates.

Fruit

PlumPeach

Nectarine

virtual public virtual private

Page 33: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 33 -

PolymorphismPolymorphism

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.

Page 34: Copyright 2006 Oxford Consulting, Ltd1 February2006 - 1 - Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.

Copyright 2006 Oxford Consulting, Ltd

1 February2006

- 34 -

PolymorphismPolymorphism

Virtual Base Classes - Initialization

Specify class E…class E : public D, public C, public virtual F

A

virtualvirtual

B C

D

E

F virtual

virtual base class