Virtual Base Class
Let consider a condition where all the three categories of inheritance, those are multiple, multilevel and hierarchical inheritance are included. This is description in figure. The 'child' has two direct base classes 'parent1' and 'parent2' that they have a general base class 'grandparent'. The 'child' inherits the traits of 'grandparent' through two separate paths. It can also inherit directly as displays through the broken line. A 'grandparent' is sometimes referred to as indirect base class.
Inheritance through the 'child' as shown in figure might pose a few problems. All the public and protected members of 'grandparent' are inherited into 'child' double, first through 'parent1' and again through 'parent2'. That means, 'child' would have duplicate sets of the members inherited from 'grandparent'. That will introduces ambiguity and should be prevented.
A duplication of inherited members due to these multiple paths can be prevents via making the general base class (ancestor class) as virtual base class although declaring the direct or intermediate base classes as display below:
Class A //grandparent
{
.......
.......
};
class B1 : virtual public A // parent1
{
...........
..........
};
class B2 : public virtual A // parent2
{
.............
..............
};
class C : public B1,public B2 //child
{
...... //only one copy of A
...... // will be inherited
};
whenever a class is made a virtual base class, C++ language takes necessary care to see which have just one copy of that class is inherited, apart from of how many inheritance paths exist among the virtual base class and a derived class. Remember that the keywords virtual and public might be used in either order.
For example, let consider again the student results processing system. Suppose that the class sports derive the roll_number from the class student. So, the inheritance relationship will be display above. A program to implement the concept of virtual base class is described in the program.
#include<iostream.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout << "Roll No :" << roll_number << "\n";
}
};
class test:virtual public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
patr2=y;
}
void put_marks(void)
{
cout << "Marks Obtained :" <<"\n";
cout << "Part1 = " << part1 << "\n";
cout << "Part2 = " << part2 << "\n";
}
};
class sports : public virtual student
{
protected:
float score;
public :
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout << "sports wt:" <<score << "\n\n";
}
};
class result : public test,public sports
{
float total;
public:
void display(void);
};
void result :: display(void)
{
}
main()
{
total = part1 +part2 +score;
put_number();
put_marks();
put_score();
cout << "Total Score :" << total << "\n";
result studnet_1;
student_1.get_number(678);
student_1.get_marks(30.5,25.5);
student_1.get_score(7.0);
stduent_1.display();
}
The output would be
Roll No: 678
Marks Obtained:
Part1 = 30.5
Part2 = 25.5
Sport wt: 7
Total Score: 63