Aae oop xp_10


Published on

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Aae oop xp_10

  1. 1. Dynamic PolymorphismObjectivesIn this lesson, you will learn to: Implement the concept of binding Use virtual functions Use pure virtual functions to create abstract classes Implement dynamic polymorphism by using late binding©NIIT OOPS/Lesson 10/Slide 1 of 15
  2. 2. Dynamic PolymorphismDynamic Polymorphism Refers to any entity changing its form, depending on circumstances©NIIT OOPS/Lesson 10/Slide 2 of 15
  3. 3. Dynamic PolymorphismBinding Is the process of associating a function with a class by identifying the type of the object or pointer that is used to invoke the function©NIIT OOPS/Lesson 10/Slide 3 of 15
  4. 4. Dynamic PolymorphismBinding (Contd.)Example: calc_net_salary() Employee class calc_net_salary() calc_net_salary() Trainee Class Confirmed Class©NIIT OOPS/Lesson 10/Slide 4 of 15
  5. 5. Dynamic PolymorphismDynamic Binding Is done during runtime Is also called late binding©NIIT OOPS/Lesson 10/Slide 5 of 15
  6. 6. Dynamic PolymorphismVirtual Function Is a function that is declared as virtual in a base class and is redefined by a derived class©NIIT OOPS/Lesson 10/Slide 6 of 15
  7. 7. Dynamic PolymorphismUsing Virtual Functions Example: class Employee { . . virtual int calc_net_salary(); . . }; class Contract:public Employee { . .©NIIT OOPS/Lesson 10/Slide 7 of 15
  8. 8. Dynamic PolymorphismUsing Virtual Functions (Contd.) int calc_net_salary(); . . . }; class Direct_Contract: public Contract { . . int calc_net_salary(); . . };©NIIT OOPS/Lesson 10/Slide 8 of 15
  9. 9. Dynamic PolymorphismPure Virtual Function Is a function without a body Is created by adding the notation ‘=0’ to the virtual function declaration Example: virtual int calc_net_salary()=0;©NIIT OOPS/Lesson 10/Slide 9 of 15
  10. 10. Dynamic PolymorphismAbstract Class Is a class containing one or more pure virtual functions Is used as a base class for deriving specific classes of the same kind©NIIT OOPS/Lesson 10/Slide 10 of 15
  11. 11. Dynamic PolymorphismStatic vs Dynamic Polymorphism Static polymorphism Is considered more efficient Is function overloading, which is bound by scopes - local and class scope©NIIT OOPS/Lesson 10/Slide 11 of 15
  12. 12. Dynamic PolymorphismStatic vs Dynamic Polymorphism (Contd.) Dynamic polymorphism Is considered more flexible Is based on overriding principles, which, therefore, is purely class scope and is based on inheritance©NIIT OOPS/Lesson 10/Slide 12 of 15
  13. 13. Dynamic PolymorphismProblem Statement 8.D.1Create an application that: Accepts customer details Accepts dealer details Displays customer details Displays dealer details©NIIT OOPS/Lesson 10/Slide 13 of 15
  14. 14. Dynamic PolymorphismSummaryIn this lesson, you learned that: The instances of an abstract class cannot be created but pointers to it can be created A function exhibits dynamic polymorphism when it exists in more than one form, and calls to its various forms are resolved dynamically when the program is executed Static binding is considered to be more efficient while dynamic binding is more flexible©NIIT OOPS/Lesson 10/Slide 14 of 15
  15. 15. Dynamic PolymorphismSummary (Contd.) Dynamic polymorphism refers to an entity changing its form, depending on circumstances The virtual keyword is used to declare a function as virtual Virtual functions that have no body are called pure virtual functions A class containing one or more pure virtual functions is called an abstract class©NIIT OOPS/Lesson 10/Slide 15 of 15