Page 236 - DCAP404 _Object Oriented Programming
P. 236

Unit 10: Virtual Functions and Polymorphism




              An abstract base class will not be used to create object, but exist only to act as a base class  Notes
               of other classes.

              Polymorphism allows the program to use the exactly same function name with exactly
               same arguments in both a base class and its subclasses. It refers to the implicit ability of a
               function to have different meanings in different contexts.

          10.6 Keywords


          Abstract Base Class: An abstract base class will not be used to create object, but exist only to act
          as a base class of other classes.
          Late Binding: Selecting functions during the execution. Though late  binding requires some
          overhead it provides increased power and flexibility.
          Polymorphism: Polymorphism allows the program to use the exactly same function name with
          exactly same arguments in both a base class and its subclasses.
          Virtual Function: Virtual functions, one of advanced features of OOP is one that does not really
          exist but it appears real in some parts of a program.
          10.7 Review Questions


          1.   Make the distinction between virtual functions and virtual base class?
          2.   How can C++ achieve dynamic binding yet also static typing?
          3.   What is the difference between virtual and non-virtual member functions?
          4.   What happens in the hardware when we call  a virtual function? How  many layers  of
               indirection are there? How much overhead is there?
          5.   How can a member function in my derived class call the same function from its base class?
          6.   Write a program which uses a polymorphism with pointers.

          7.   Are virtual functions hierarchical? Give an example in support of your answer.
          8.   Write a program to use constructors and destructors in inheritance.
          9.   Debug the following program:

                    class  one
                    {
                         int  a;
                    public:
                         void  set(int  a)
                         {
                                a  =  a;

               }
                         void  show()
                         {
                         cout  <<  a;





                                           LOVELY PROFESSIONAL UNIVERSITY                                   229
   231   232   233   234   235   236   237   238   239   240   241