1 / 9

Polymorphism

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.

Download Presentation

Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Polymorphism

  2. 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. 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. 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. 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. 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. 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. 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; }

  9. 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; }

More Related