AppDividend
Latest Code Tutorials

C++ Virtual Base Class Example Tutorial

0

C++ Virtual Base Class Example Tutorial is today’s topic. Virtual base classes come in handy during multiple inheritance. During inheritance, it is quite common to receive multiple instances of a single base class via different routes which can create ambiguity. Due to the fact that the sub child class receives multiple instances of the same base class when inheriting from more than one child classes, confuses the compiler and it shows an error message for the same.

C++ Virtual Base Class

The problem with regular base classes is that while inheriting them, we can have multiple child classes from them. Then after some point, those multiple child classes can be further inherited together by a single sub-level child class. In that case, the sub-level child class receives multiple instances of the same base class via different child classes.

To curb this problem, we use virtual base classes. Virtual base classes as the name suggests can create multiple duplicate copies of its data members and member function to distribute them among the various child classes. And when these different child classes get inherited by the sub-child class, only a single original copy of data members and member function of the base class reaches the sub-child class, eliminating any confusion for the compiler.

See the following syntax.

class child_classname:virtual public parent_classname {};

See the following example.

class Child1 : virtual public Parent {};
class Child2 : public virtual Parent {};

(Note that the “virtual” keyword can be mentioned in the declaration of the base class either before the “public” keyword or after it.)

#Program to declare Destructors

See the following code file.

#include <iostream>
using namespace std;

class Base_class
{
public:
    int data_member;
    int member_function()
    {
        cin >> data_member;
        cout << "The data member from Base class is " << data_member << endl;
        return 0;
    }
};

class Child_class1 : public virtual Base_class
{
};

class Child_class2 : public virtual Base_class
{
};

class Sub_class : public Child_class1, public Child_class2
{
};

int main()
{
    Sub_class object1;
    object1.member_function();

    return 0;
}

See the following output.

 

C++ Virtual Base Class

#The problem with usual Base Classes

See the following program.

#include <iostream>
using namespace std;

class Base
{
public:
    void function()
    {
        cout << "Base class function executed";
    }
};

class Child1 : public Base
{
};

class Child2 : public Base
{
};

class Subchild : public Child1, public Child2
{
};

int main()
{
    Subchild obj1;
    obj1.function();
}

In the output, A compiler error will be there, refer the below image.

 

 

C++ Virtual Base Class Example

#Explanation

Here, we have a single base class “Base” which is getting inherited by two child classes named “Child1” and “Child2”. So each of this child class gets their own separate copy of the data members and the member functions of the base class.

Further, these two child classes are inherited by a single sub-child class “Sub child”.

This leads to the sub-child class receiving two copies of the data members and the member functions of the base class each through both child classes.

During compilation, this creates an ambiguity. The compiler gets confused about which data member or member function the sub child class is pointing because both of them have the same names but still are different copies. All this chaos leads to an error message by the compiler in the output stream.

This can be solved by making the child class virtual. So that only duplicate entries of the same data members and member functions get used in the various inherited child classes. With these, the final sub-child class receives only the same copy of the data through different means leading to no confusion.

Finally, C++ Virtual Base Class Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.