C++ Virtual function accessed from pointer

Description

C++ Virtual function accessed from pointer

#include <iostream>
using namespace std;
class Base//w ww  . j  a  va 2 s.c  o m
{
   public:
   virtual void show()         //virtual function
   { cout << "Base\n"; }
};
class Derv1 : public Base         //derived class 1
{
   public:
   void show()
   { cout << "Derv1\n"; }
};
class Derv2 : public Base         //derived class 2
{
   public:
   void show()
   { cout << "Derv2\n"; }
};
int main()
{
   Derv1 dv1;           //object of derived class 1
   Derv2 dv2;           //object of derived class 2
   Base* ptr;           //pointer to base class
   ptr = &dv1;          //put address of dv1 in pointer
   ptr->show();         //execute show()
   ptr = &dv2;          //put address of dv2 in pointer
   ptr->show();         //execute show()
   return 0;
}



PreviousNext

Related