C++ program to illustrate virtual base classes

#include<iostream>
using namespace std;
class Base1

{

       public :

              void showbase1( )

              {

                 cout<<endl<<"base1";

              }

};

class Base2 : virtual public Base1

{

     public:

             void showbase2( )

             {

                cout<<endl<<"base2";

             }

};

class Base3 : virtual public Base1

{

      public:

              void showbase3( )

              {

                  cout<<endl<<"base3";

              }

};

class Derived : public Base2, public Base3

{

      public:

              void showderived( )

              {

                  cout<<endl<<"derived";

              }

};

 

int main( )

{

   Derived d;

   d.showbase1( );

   d.showbase2( );

   d.showbase3( );

   d.showderived( );

   return 0;

}

Output

base1

base2

base3

derived

Leave a Reply

Your email address will not be published.