lecture04 polymorphism

of 28 /28
TCP1201 OOPDS Lecture 4 1

Author: hariz-mustafa

Post on 19-Nov-2014




1 download

Embed Size (px)




  • 1. Lecture 4TCP1201 OOPDS 1

2. Learning ObjectivesTo understand static polymorphism and dynamicpolymorphismTo understand the problem of staticpolymorphismTo enable dynamic polymorphism via virtualfunctionTo understand the benefit of dynamicpolymorphismTo understand abstract classTo understand why virtual destructor is needed 2 3. What is Polymorphism?-Polymorphism is allowing objects of different typesto respond differently to the same method call.-Polymorphism occurs when there is functionoverriding. - In lecture 3, superclass Human has a speak method and is overridden by subclass Student. Invoking the speak method from Human has different result than Invoking the speak method from Student. 3 4. Different objects invoke different version of methodswith the same name (speak())class Animal { public:void speak() {How does C++ knows cout speak(); // call Bird::speak()delete a;delete b;4} 5. Function Call Binding Connecting a function call to a function body is called binding Function call binding is the process of determining what blockof function code is executed when a function call is made. class Animal {Memorypublic: void speak() { ... } }; Animal::speak()0x7723 class Bird : public Animal {Definitionpublic: void speak() { ... } }; Bird::speak()0x77b4 int main() { Definition Animal* a = new Animal; a->speak(); Bird* b = new Bird; b->speak(); delete a; delete b; 5 } 6. Function Call BindingThere are 2 types of function call binding(polymorphism):Early/Static/Compile-timebinding/polymorphism: Binding is performedduring compile-time, and is decided by the compilerand linker based on the variable used to invoke themethod.Late/Dynamic/Runtime binding/polymorphism:Binding occurs at runtime, based on the type of theobject calling the method. 6 7. Static Polymorphism It is the polymorphism that is implemented usingstatic binding. The compiler determines beforehand whatmethod definition will be executed for a particularmethod call, by looking at the type of the variableinvoking the method. We have been using static polymorphism thus farwithout realizing it.7 8. Static PolymorphismConsider the following Animal hierarchy, subclassBird overrides superclass Animals move()method.class Animal {Animal public:void move() { cout move(); // cout "Moving".Animal& a3 = a1; // Reference. Animal::move() is called.a3.move(); // cout "Moving". No problemBird b1;b1.move(); // cout "Flying".Bird *b2 = new Bird; // Pointer.b2->move(); // cout "Flying".Bird& b3 = b1;Bird::move() is called. Nob3.move(); // cout "Flying". problem9} 10. Static Polymorphism: ProblemCall the move() method from a Bird object using anAnimal pointer/reference, got overriding and gotupcasting => big problem.int main() {Bird* b1 = new Bird;Animal* a1 = b1; // Upcasting via pointer.a1->move(); // cout "Moving".Bird b2;a1 = &b2; // Upcasting via pointer.a1->move(); // cout "Moving".Animal& a2 = b2; // Upcasting via reference.a2.move(); // cout "Moving".delete b1;}Animal::move() is still called but Bird::move() is more appropriate/precise/accurate in the context.10 11. Static Polymorphism: ProblemConsider the following inheritance hierarchy:class Animal { public: void move() { cout