Download presentation
Presentation is loading. Please wait.
Published byOscar Bates Modified over 9 years ago
1
CMSC 2021 Polymorphism
2
CMSC 2022 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.
3
CMSC 2023 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
4
CMSC 2024 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
5
CMSC 2025 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
6
CMSC 2026 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
7
CMSC 2027 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]); }
8
CMSC 2028 Static vs. Dynamic Binding Example (con’t) void virtualViaPointer(const Shape *baseClassPtr) { // Dynamic binding takes place baseClassPtr->printShapeName(); baseClassPtr->print(); cout computeArea() << endl computeVolume() << endl; }
9
CMSC 2029 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.