Thursday, March 28, 2013

Virtual Function – C++.




The functions which can be overridden within inheriting class is known as virtual functions, this also follows the polymorphism principle of object-oriented programming. A virtual function is a member function that is declared within a base class and redefined by a derived class. A virtual function should precede with keyword 'virtual'.

Pure Virtual function : Pure virtual function is a that implemented in the derived class. Pure virtual functions have no body in the base classes and have to overloaded in the derived classes.

The pure virual function in the base class define as :

virtual void show()=0;

Example  of Virtual Function:

#include <iostream.h>
  class Base
      {
           protected:
              int a;
           public:
                   Base(int a)
                        {
                            this->a=a;
                        }
                   void showData()
                        {
                            cout<<"\n\tValue in Base Class  : "<
                        }
      };
  class Derived:public Base
      {
           public:
              int b;
              Derived(int x,int y):Base(x)
                   {
                        b=y;
                   }
              void showData()
                   {
                        cout<<"\n\t Value-1 in Derived class : "<
                        cout<<"\n\t Value-2 in Derived class : "<
                   }
      };
  int main()
      {
           int n;
           Base *bptr;      //Base Pointer
           Base Bs(10);
           bptr=&Bs;     //Base Address
           bptr->showData();
           Derived Dr(10,20);
           bptr=&Dr;
           bptr->showData(); //showData() from Base class
           Derived *dptr;
           dptr = &Dr;
           dptr->showData(); //showData() from derived class
           ((Derived *)bptr)->b=500;
           ((Derived *)bptr)->showData();
           return 0;
      }

Example  of Pure Virtual Function:



#include <iostream.h>

  class Base
      {
           public:
              //pure virtual Function (without Body)
              virtual void showData()=0;
      };
  class Derived_1:public Base
      {
           public:
              void showData()
                   {
                        cout <<"\n\tIn First Derived Class";
                   }
      };
  class Derived_2:public Base
      {
           public:
              void showData()
                   {
                        cout <<"\n\tIn Second Derived Class";
                   }
      };

  class Derived_3:public Base
      {
           public:
              void showData()
                   {
                        cout <<"\n\tIn Third Derived Class";
                   }
      };

  int main()
      {
            int i;
            Base *B[3];
            Derived_1 D1;
            Derived_2 D2;
            Derived_3 D3;
            B[0]=&D1;
            B[1]=&D2;
            B[2]=&D3;
            for(i=0;i<3 i="" span="">
              {
                   B[i]->showData();
              }
           return 0;
      }


No comments: