Polymorphism CMSC 202 1 Static vs Dynamic Binding
Polymorphism CMSC 202 1
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. CMSC 202 2
Static Binding • Compiler can determine • For example, Time t 1; Ext. Time et 2; t 1. Set(); et 1. Set(); // static binding t 1. Write(); et 1. Write(); // static binding CMSC 202 3
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 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 CMSC 202 4
Static vs. Dynamic Binding Example Class Attributes Shape Methods - virtual compute. Area - virtual compute. Volume - pure virtual print. Shape. Name - pure virtual print Point x y - virtual print. Shape. Name - virtual print - constructor - set. Point - get. X - get. Y Circle radius - virtual print. Shape. Name - virtual print - virtual compute. Area - set. Radius - get. Radius - constructor Cylinder height - virtual print. Shape. Name - virtual print - virtual compute. Area - virtual compute. Volume - set. Height - get. Height - constructor CMSC 202 5
Static vs. Dynamic Binding Example (con’t) void virtual. Via. Pointer(const Shape*); void virtual. Via. Reference(const Shape &); void main() { Point point(7, 11); Circle circle(3. 5, 22, 8); Cylinder cylinder(10, 3. 3, 10); point. print. Shape. Name(); // static binding point. print(); cout << endl; // static binding circle. print. Shape. Name(); // stataic binding circle. print(); cout << endl; // static binding cylinder. print. Shape. Name(); // static binding cylinder. print(); cout << endl; // static binding CMSC 202 6
Static vs. Dynamic Binding Example (con’t) Shape *array. Of. Shapes[3]; // array of base class pointers array. Of. Shapes[0] = &point; array. Of. Shapes[1] = &circle; array. Of. Shapes[2] = &cylinder; for (int I = 0; I < 3; I++) virtual. Via. Pointer(array. Shapes[I]); for (int j = 0; j < 3; j++) virtual. Via. Reference(*array. Shapes[j]); } CMSC 202 7
Static vs. Dynamic Binding Example (con’t) void virtual. Via. Pointer(const Shape *base. Class. Ptr) { // Dynamic binding takes place base. Class. Ptr->print. Shape. Name(); base. Class. Ptr->print(); cout << “Area = “ << base. Class. Ptr->compute. Area() << endl << “Volume = “ << base. Class. Ptr->compute. Volume() << endl; } CMSC 202 8
Static vs. Dynamic Binding Example (con’t) void virtual. Via. Reference(const Shape &base. Class. Ref) { // Dynamic binding takes place base. Class. Ref. print. Shape. Name(); base. Class. Ref. print(); cout << “Area = “ << base. Class. Ref. compute. Area() << endl; << “Volume = “ << base. Class. Ref. compute. Volume() << endl; } CMSC 202 9
- Slides: 9