What is Virtual Base Class in C++?

Take a situation where
all the three types of inheritance, multiple, multilevel and hierarchical inheritance
are used together. This situation is illustrated in above image.

The child has two base classes parent1
and parent2 and these two have a
common base class grandparent. All
the public and protected members of grandparent
are inherited into child twice,
first from parent1 and again from parent2. This means that child have duplicate sets of members
inherited from grandparent. It
causes ambiguity in the program.

This kind of problem can
be resolved by making the common base class as a virtual base class. It can be
done in following way:

                                                                                                                                                                            

class grandparent

{

                            . . . . . .

                            . . . . . .

};

class parent1: virtual
public grandparent

{

                            . . . . . .

                            . . . . . .

};

class parent2: public
virtual grandparent

{

                            . . . . . .

                            . . . . . .

};

class child: public
parent1, public parent2

{

                            . . . . . .

                            . . . . . .

};

If a class is made a virtual
base class, only one copy of that class is inherited in derived class. Remember
one thing that the keyword virtual and public may be used in either order.

Below I have written a program
that implements the concept of virtual base class.

What is Virtual Base Class in C++?
#include<iostream>

using namespace std;

class student

{

    protected:

    int roll_no;

    public:

    void get_no(int x)

    {

        roll_no=x;

    }

    void put_no()

    {

        cout<<“Roll
Number:”<<roll_no;

    }

};

class test: virtual public
student

{

    protected:

    float sub_marks;

    public:

    void get_submarks(float y)

    {

        sub_marks=y;

    }

    void put_submarks()

    {

        cout<<“nSubject
Marks:”<<sub_marks;

    }

};

class sports: public
virtual student

{

    protected:

    float sp_marks;

    public:

    void get_spmarks(float z)

    {

        sp_marks=z;

    }

    void put_spmarks()

    {

        cout<<“nSports
Marks:”<<sp_marks;

    }

};

class result: public test,
public sports

{

    float total_marks;

    public:

    void put_result()

    {

        total_marks=sub_marks+sp_marks;

        put_no();

        put_submarks();

        put_spmarks();

        cout<<“nTotal
Marks:”<<total_marks;

    }

};

int main()

{

    result R;

    R.get_no(20);

    R.get_submarks(75.6);

    R.get_spmarks(81.2);

    R.put_result();

    return 0;

}

In the above example student class is an abstract class, as it is not used to create any object. It is used only for deriving other classes.

If you liked this article
then don’t forget to comment and share.

Leave a Reply

Your email address will not be published. Required fields are marked *