Virtual base class in C++

In this tutorial, we will be discussing a program to understand virtual base class in C++.

Virtual classes are primarily used during multiple inheritance. To avoid, multiple instances of the same class being taken to the same class which later causes ambiguity, virtual classes are used.


 Live Demo

#include <iostream>
using namespace std;
class A {
   int a;
      a = 10;
class B : public virtual A {
class C : public virtual A {
class D : public B, public C {
int main(){
   //creating class D object
   D object;
   cout << "a = " << object.a << endl;
   return 0;


a = 10

Updated on: 12-Mar-2020

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started