Title: Polymorphism
1Polymorphism
2Static vs. Dynamic Binding
- Binding
- The determination of which method in the class
- hierarchy is to be used for a particular object.
- Static (Early) Binding
- When the compiler can determine which method in
the - class hierarchy to use for a particular object.
- Dynamic (Late) Binding
- When the determination of which method in the
class - hierarchy to use for a particular object occurs
during - program execution.
3Static Binding
- Compiler can determine
- For example,
- Time t1
- ExtTime et2
- t1.Set() // static binding
- et1.Set() // static binding
- t1.Write() // static binding
- et1.Write() // static binding
4Dynamic Binding
- Compiler cannot determine binding of object to
method - Binding is determined dynamically at runtime
- To indicate that a method is to be bound
dynamically, - must use the reserved word virtual
- virtual void Write() // dynamic binding
will occur - When a method is defined as virtual, all
overriding methods - from that point on down the hierarchy must be
defined as - virtual
- A pure virtual method is defined as
- virtual void Write() const 0
- When a class contains a pure virtual method, the
class is - truly abstract I.e., it cannot be instantiated
5Static vs. Dynamic Binding Example
Class Attributes
Methods Shape - virtual
computeArea - virtual computeVolume
- pure virtual printShapeName
- pure virtual
print Point x - virtual
printShapeName y -
virtual print - constructor
- setPoint - getX -
getY Circle radius - virtual
printShapeName -
virtual print - virtual computeArea
- setRadius - getRadius
- constructor Cylinder height - virtual
printShapeName - virtual print
- virtual computeArea - virtual
computeVolume -
setHeight - getHeight - constructor
6Static vs. Dynamic Binding Example (cont)
void virtualViaPointer(const Shape) void
virtualViaReference(const Shape ) void main()
Point point(7, 11) Circle circle(3.5,
22, 8) Cylinder cylinder(10, 3.3, 10, 10)
point.printShapeName() // static
binding point.print() cout ltlt endl //
static binding circle.printShapeName()
// stataic binding circle.print() cout ltlt
endl // static binding cylinder.printShape
Name() // static binding
cylinder.print() cout ltlt endl // static
binding
7Static vs. Dynamic Binding Example (cont)
Shape arrayOfShapes3 // array of base
class pointers arrayOfShapes0 point
arrayOfShapes1 circle arrayOfShapes2
cylinder for (int I 0 I lt 3 I)
virtualViaPointer(arrayShapesI) for (int j
0 j lt 3 j) virtualViaReference(arraySh
apesj)
8Static vs. Dynamic Binding Example (cont)
void virtualViaPointer(const Shape
baseClassPtr) // Dynamic binding takes
place baseClassPtr-gtprintShapeName()
baseClassPtr-gtprint() cout ltlt Area ltlt
baseClassPtr-gtcomputeArea() ltlt endl ltlt
Volume ltlt baseClassPtr-gtcomputeVolume()
ltlt endl
9Static vs. Dynamic Binding Example (cont)
void virtualViaReference(const Shape
baseClassRef) // Dynamic binding takes
place baseClassRef.printShapeName()
baseClassRef.print() cout ltlt Area ltlt
baseClassRef.computeArea() ltlt endl ltlt
Volume ltlt baseClassRef.computeVolume()
ltlt endl