What is a Virtual base class?
The virtual base class is very helpful in virtual inheritance. When we use multiple inheritances, then multiple “instances” of a given class appearing in an inheritance hierarchy. It leads to redundancy. Redundancy is not good in such a scenario. When we declare a class as a virtual class, then it means that we are helping the compiler that we only required a single instance.Why we use a virtual base class?
Virtual base class means that child classes can access the base classes virtually from a long distance in the parent-child hierarchy.How to declare a class as a virtual base class?
class A { public: int number; A() { int number = 10; } }; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C{}; Explanation of Diagrams Class A:Base Parent Class Class B: B is child class of class A. Class C: C is a child class of class A. Class D: D is a child class of class B and Class C. A is the grandfather of class D. Step 1:- Create class A
- declare the variable “int number” inside class A.
- Class C and Class B get the “int number” variable from class A.
- Class D is getting the “int number” from both class B, and Class C. This is not good to do.



C++ Program of Virtual base class
Let us code the above-mentioned scenario with C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; class A { public: int number; A() // constructor { number = 10; } }; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C{}; int main() { D object; // object is created for class d cout << "number = " << object.number << endl; return 0; } |
Example 2 with error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; class T4TUTORIALS_ONE { public: int n1; }; class T4TUTORIALS_TWO : public T4TUTORIALS_ONE { public: int n2; }; class T4TUTORIALS_THREE : public T4TUTORIALS_ONE { public: int n3; }; class T4TUTORIALS_FOUR : public T4TUTORIALS_TWO, public T4TUTORIALS_THREE { public: int n4; }; int main() { T4TUTORIALS_FOUR obj; obj.n1 = 40; //error obj.n1 = 30; // error obj.n2 = 60; obj.n3 = 70; obj.n4 = 80; cout<< " T4TUTORIALS_ONE : "<< obj.n1 // error cout<< " T4TUTORIALS_TWO : "<< obj.n2; cout<< " T4TUTORIALS_THREE: "<< obj.n3; cout<< " T4TUTORIALS_FOUR: "<< obj.n4; } |
Example 2 without error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; class T4TUTORIALS_ONE { public: int n1; }; class T4TUTORIALS_TWO : virtual public T4TUTORIALS_ONE { public: int n2; }; class T4TUTORIALS_THREE : virtual public T4TUTORIALS_ONE { public: int n3; }; class T4TUTORIALS_FOUR : public T4TUTORIALS_TWO, public T4TUTORIALS_THREE { public: int n4; }; int main() { T4TUTORIALS_FOUR obj; obj.n1 = 40; obj.n1 = 30; obj.n2 = 60; obj.n3 = 70; obj.n4 = 80; cout<< " T4TUTORIALS_ONE : "<< obj.n1; cout<< " T4TUTORIALS_TWO : "<< obj.n2; cout<< " T4TUTORIALS_THREE: "<< obj.n3; cout<< " T4TUTORIALS_FOUR: "<< obj.n4; } |
Video Lecture
Virtual Base Class Exercise Solution
- Write a C++ Program to display the reverse of a number using the Virtual base Class in Object Oriented Programming(OOP).
- Write a program in C++ to convert a decimal number into binary without using an array and using the Virtual base Class in Object Oriented Programming(OOP).
- Write a C++ program to find HCF (Highest Common Factor) of two numbers using the Virtual base Class in Object Oriented Programming(OOP).
- C++ program to print rhombus or parallelogram star pattern using the Virtual base Class in Object Oriented Programming(OOP).
- Write a C program to check whether a number is a Strong Number or not using the Virtual base Class in Object Oriented Programming(OOP).
- Write a program in C++ to find the prime numbers within a range of numbers by using the Virtual base Class in Object Oriented Programming(OOP).
- Write a program in C++ to convert a decimal number into octal without using an array and Virtual base Class in Object Oriented Programming(OOP).
- Write a C++ program to display Pascal’s triangle using Virtual base Class in Object Oriented Programming(OOP).
- C++ program to print hollow rhombus, parallelogram star pattern using the Virtual Base Class.
- C++ program to print mirrored right triangle star pattern using virtual base class.
- Write a program in C++ to display the sum of the series by using the virtual base class.
- Write a C++ program to find Strong Numbers within a range of numbers by using the virtual base class.
- Write a program in C++ to display the cube of the number up to a given integer by using the virtual base class.
- Write a program in C++ to check Armstrong number of n digits by using the virtual base class.
- Write a program in C++ to print a string in reverse order by using the virtual base class.
- Write a C++ program to find the perfect numbers within a given number of range by using the virtual base class.
- Write a C++ Program to display the pattern like a pyramid using the alphabet by using the virtual base class. Solution
- Write a program in C++ to display the n terms of odd natural number and their sum. Solution
- Write a program in C++ to make such a pattern like a pyramid with a number that will repeat the number in the same row.
- Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers by using the virtual base class.
- Develop a C++ program to print a hollow square or rectangle star pattern by using the virtual base class.
- Write a program in C++ to convert an Octal number to a Decimal number by using the virtual base class.
- Write a program in C++ to find the number and sum of all integer between 100 and 200 which are divisible by 9.
- Write a program in C++ to make such a pattern like a pyramid with numbers increased by 1 by using the virtual base class.
- C++ program to print the hollow right triangle star pattern by using the virtual base class in object-oriented programming.
- Write a C++ program to find out the sum of an A.P. series by using the virtual base class.
- Write a program in C++ to display the pattern like a diamond by using the virtual base class.
- Fibonacci Series Program in C++ with virtual inheritance.