Virtual base class in C++
Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A. Then class D will inherit properties from class B and class C. An error will occur when we run the code because class D will have twice the properties of class A. In such a situation, we can use the keyword Virtual.
What is Virtual Class?
A Virtual Class is a keyword used in the derived Class. This keyword ensures that only one copy of the parent class is in the derived Class. This virtual Class helps to reduce the error caused in the above example. Specifying a class as a virtual base class prevents duplication of its data members. This allows all base classes that utilize the virtual base class to share only one copy of all the data members.
Example: Error when Class is inherited twice
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Constructor A\n";
}
void display() {
cout << "Hello form Class A \n";
}
};
class B: public A {
};
class C: public A {
};
class D: public B, public C {
};
int main() {
D object;
object.display();
}
Output:
Request for member 'display' is ambiguous
object.display();
Explanation:
In the above code, we have considered 4 classes, class A, class B, Class C, and Class D. If the class B and class c inherit properties from class A. Then class D will inherit properties from class B and class C. When we run the code, the error occurs because class D will have twice the properties of class A.
The syntax for declaring virtual base:
class B: virtual public A {
// statement 1
};
class C: public virtual A {
// statement 2
};
Example:
#include <iostream>
using namespace std;
class A {
public:
A() // Constructor
{
cout << "Hi from Constructor A\n";
}
};
//Class is inherited using the virtual keyword
class B: public virtual A {
};
//Class is inherited using the virtual keyword
class C: public virtual A {
};
class D: public B, public C {
};
int main() {
D object; // Object creation of class D.
return 0;
}
Output:

Explanation:
In the above example, we created 4 classes, the same as the last example, and we also inherited the classes similar to the last example. But here, we used the virtual keyword, which has created a single copy of class A in Class D. Due to this, no error is generated.
Example:
#include <iostream>
using namespace std;
class A {
public:
int a;
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;
}
Output:

Explanation:
In the above example, we have created four classes A, B, C, and D. Using the virtual keyword, we have inherited the properties of A into B and C. Then we have inherited the B and C properties into D here as we have used the virtual keyword so no duplicate class will be created into D, and no error will have occurred.
- To ensure that all base classes are created before their derived classes, virtual base classes are always created before non-virtual base classes.
- Objects of classes B and C still have calls to class A, but they are ignored when creating objects of Class D. Objects of classes B and C class have the constructor of class A
Pure Virtual Function: A normal virtual function describes the base class with nothing known as a pure virtual function.
Example:
#include <iostream>
using namespace std;
class Animal {
public:
// Pure Virtual Function is created inside the parent class
virtual void move() = 0;
};
class Lion: public Animal {
public:
void move() {
cout << "Hi from the Lion class" << endl;
}
};
class Wolf: public Animal {
public:
void move() {
cout << "Hi from the wolf class" << endl;
}
};
int main() {
Lion l;
Wolf w;
l.move();
w.move();
}
Output:

Explanation:
We have inherited the base class properties into the other two classes. In the above example, we have created a parent class, Animal, in this Class we have defined Virtual function which is know as pure virtual. In the above Class, we have defined a pure virtual class, so we need to define the function in the derived classes.