C++ program to illustrate virtual functions

#include<iostream>
using namespace std;
class B

{

     public:

             virtual    void display()

             {

                cout<<endl<<"B's show";

             }

};

class D : public B

{

     public:

              void display()

              {

                  cout<<endl<<"D's show";

              }

};

 

int main()

{

     B *ptr;

     B b;

     ptr=&b;

     ptr->display();

     D d;

     ptr=&d;

     ptr->display();

     return 0;

}

Output

B’s show

D’s show

Leave a Reply

Your email address will not be published.