100 likes | 129 Views
Polymorphism. Static 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.
E N D
Static 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.
Static 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
Dynamic 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
Static 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
Static vs. Dynamic Binding Example (con’t) 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 << endl; // static binding circle.printShapeName(); // stataic binding circle.print(); cout << endl; // static binding cylinder.printShapeName(); // static binding cylinder.print(); cout << endl; // static binding
Static vs. Dynamic Binding Example (con’t) Shape *arrayOfShapes[3]; // array of base class pointers arrayOfShapes[0] = &point; arrayOfShapes[1] = &circle; arrayOfShapes[2] = &cylinder; for (int I = 0; I < 3; I++) virtualViaPointer(arrayShapes[I]); for (int j = 0; j < 3; j++) virtualViaReference(*arrayShapes[j]); }
Static vs. Dynamic Binding Example (con’t) void virtualViaPointer(const Shape *baseClassPtr) { // Dynamic binding takes place baseClassPtr->printShapeName(); baseClassPtr->print(); cout << “Area = “ << baseClassPtr->computeArea() << endl << “Volume = “ << baseClassPtr->computeVolume() << endl; }
Static vs. Dynamic Binding Example (con’t) void virtualViaReference(const Shape &baseClassRef) { // Dynamic binding takes place baseClassRef.printShapeName(); baseClassRef.print(); cout << “Area = “ << baseClassRef.computeArea() << endl; << “Volume = “ << baseClassRef.computeVolume() << endl; }