C++ Pointer access Object functions accessed

Description

C++ Pointer access Object functions accessed

#include <iostream>
using namespace std;
class Base                       //base class
{
   public://from  w  w w.  j a  va  2 s.  com
   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;
}



PreviousNext

Related