What is the need of virtual base classes?

Q.3) a) What is the need of virtual base classes? Give an example to illustrate the need for virtual base class.

Ans.


In the given below example Test and Sports class are inherited from the Student class. Therefore all the public and protected member of student class will be part of Test and Sports class. Again Result class is derived from Test and Sports class (Multiple Inheritance). So Result class has two copies of data member and member function of Student class. This will arise ambiguity. The solution of this problem is virtual base class. If we declare Student class as a virtual class while inheriting it, then only one copy will be inherited in Result class.



b) Write a program to read text file and count number of characters in it.

Ans.


Post your comment