Normal functions accessed from pointer - C++ Class

C++ examples for Class:Class Pointer

Description

Normal functions accessed from pointer

Demo Code

#include <iostream>
using namespace std;
class Base                       //base class
{
   public://from   w w w. j  a va 2 s  . c o  m
   void show()                //normal 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;
}

Result


Related Tutorials