Page 226 - DCAP404 _Object Oriented Programming
P. 226

Unit 10: Virtual Functions and Polymorphism




          2.   A virtual function is a member function of a base class and relies on a specific object to  Notes
               determine which implementation of the ………………………… is called.
          3.   If a  function is declared …………………………  in its  base class, you can still access it
               directly using the scope resolution (::) operator.
          4.   If you do not override a virtual member function in a derived class, a call to that function
               uses the function implementation defined in the…………………....
          5.   If  you  declare  a  base  class  destructor  as  virtual,  a  derived  class  destructor  will
               ………………………… that base class destructor, even though destructors are not inherited.

          10.2 Pure Virtual Functions


          Generally a function is declared virtual inside a base class and we redefine it the derived classes.
          The function declared in the base class seldom performs any task.
          The following program demonstrates how a  pure virtual  function is defined, declared and
          invoked from the object of a derived class through the pointer of the base class. In the example
          there are two classes employee and grade. The class employee is base class and the grade is
          derived class. The functions getdata ( ) and display ( ) are declared for both the classes. For the
          class employee the functions are defined with empty body or no code inside the function. The
          code is written for the grade class.

               !

             Caution  The methods of the derived class are invoked by the pointer to the base class.
          #include  <iostream.h>
          #include<conio.h>
          class  employee  {


          int  code;
          char  name  [20];

          public:
          virtual  void  getdata  (  );
          virtual  void  display  (  );
          };
          class  grade:  public  employee
          {
                 char  grd  [90];

                 float  salary;
          public:
                 void  getdata  (  );
                 void  display  (  );
          };
          void  employee  ::  getdata  ()




                                           LOVELY PROFESSIONAL UNIVERSITY                                   219
   221   222   223   224   225   226   227   228   229   230   231