C++ program to illustrate pointer to base class

#include<iostream>
using namespace std;
class Base

{

     public:

             void show()

             {

                cout<<endl<<"Base show";

             }

};

class Derived : public Base

{

     public:

              void show()

              {

                  cout<<endl<<"Derived show";

              }

};

 

 

 

 

int main()

{

     Base *ptr;

     Base b;

     ptr=&b;

     ptr->show();

     Derived d;

     ptr=&d;

     ptr->show();

     Derived *dptr;

     dptr=&d;

     dptr->show();

     return 0;

}

Output

Base show

Base show

Derived show

Leave a Reply

Your email address will not be published.