Virtual Base Class in C++ with Example Program with Pdf

0
644

Virtual Base Class in C++ with Example Program

In This Article will show you Virtual Base Class in C++ with Example Program & also You can download pdf Virtual Base Class in C++ with Example Program.

All types of inheritances are involved in virtual base classes, including multi-level, multiple, hybrid, and hierarchical inheritance.

When several base classes are inherited, uncertainty may be introduced into a C++ program. When a class is a virtual base class, C++ ensures that only one copy of the base class is inherited, regardless of how many inheritance paths exist between the virtual base class and the derived class.

To create a virtual base class, the keyword virtual should come before the derivation.

It is possible to write virtual public or virtual public.

There isn’t any distinction between them. Both definitions will be treated equally by the compiler. When the compiler encounters the keyword virtual with derivation, it ensures that no two instances of the same base class are passed down to the derived class of the base class.

Virtual Base Class in C++ with Example Program
Virtual Base Class in C++ with Example Program with Pdf
#include<iostream>
using namespace std;

class base
//base class derivation
{
  public:
  int i;
};

class derived1 : virtual public base
// derived1 class inherits base class as virtual.
{
  public:
  int j;
};

class derived2 : virtual public base
// derived2 class inherits base class as virtual.
{
  public:
  int k;
};


class derived3 : public derived1, public derived2
/* derived3 class inherits both derived1 and derived2 classes.
This time, there is only one copy of base class in derived3 class,
Because derived2 and derived3 class are inherited virtually.*/
{
  public:
  int sum;
};

int main ()
{
	derived3 ob;
  
  ob.i = 10;
  // accessing base class variable
  
  ob.j = 20;
  // accessing derived1 class variable

  ob.k = 30;
  // accessing derived2 class variable
  
  ob.sum = ob.i + ob.j + ob.k;
  // accessing its own original member
  
  cout << "i = " << ob.i ;
  cout << "\nj = " << ob.j ;
  cout << "\nk = " << ob.k ;
  cout << "\nSum of above variables is : " << ob.sum;

  return 0;
}

The keyword virtual, as we can see, comes before the rest of the inherited class’s specification. Since derived1 and derived2 have both inherited base as virtual, any multiple inheritance involving them would result in only one copy of base.

As a result, there is only one copy of base in derived3, and ob.i = 10 is completely true and unambiguous. Another thing to bear in mind is that, even though both derived1 and derived2 declare base to be virtual, base is still present in both types of objects.

The only difference between a regular base class and a virtual base class is what happens when an object inherits it several times. Only one base class is present in the object if virtual base classes are used. Otherwise, there would be several versions.

Let’s look at an example programme that demonstrates the use of virtual base classes in hybrid inheritance:

#include<iostream>
using namespace std;

class A
{
  protected:
    int x;
  
  public:
    void get (int a)
    {
      x = a;
    };
  
    void show ()
    {
      cout << x;
    };
};

class A1 : virtual public A
{
  protected:
    int y;

  public:
    void get (int a)
    {
      y = a;
    };

    void show ()
    {
      cout << y;
    };
};

class A2 : virtual public A
{
  protected:
    int z;
   
  public:
    void get (int a)
    {
      z = a;
    };
   
    void show ()
    {
      cout << z;
    };
};

class A12 : public A1 , public A2
{
  int r , t;
  
  public:
    void get (int a)
    {
      r = a;
    };

    void show ()
    {
      t = x + y + z + r;
      cout << "Result is = " << t;
    };
};

int main ()
{
  A12 r;
  r.A :: get (3);
  r.A1 :: get (4);
  r.A2 :: get (5);
  r.get (6);
  r.show();
 
  return 0;
}

/* In this example the output will be 18 which is the result. It will not display rest of the variables in the code. Run the program in the compiler and experience why. */

Virtual Base Class in C++ With Example Program Pdf

Virtual class

LEAVE A REPLY

Please enter your comment!
Please enter your name here