Diamond problem in OOPS and solution using Virtual Inheritance

Here we are talking about the Diamond Problem in OOPS ( Object oriented programming ) and solution for that using virtual inheritance n C++ Programming Language)

In this below video you will learn how diamond problem will occur when we use multiple inheritance, and how to solve that in detail with example.

Doamind Problem in oops and Solution in C++

Diamond.cpp

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Animal{
 6 public:
 7     Animal(){
 8         cout << "animal constructor" << endl;
 9     }
10     int age;
11     void walk(){
12         cout << "animal walks" << endl;
13     }
14 };
15 
16 class Tiger :virtual public Animal{
17 public:
18     Tiger(){
19         cout << "constructor of tiger" << endl;
20     }
21 };
22 
23 class Lion :virtual public Animal{
24 public:
25     Lion(){
26         cout << "constructor of Lion" << endl;
27     }
28 };
29 
30 class Liger : public Tiger , public Lion{
31 public:
32     Liger(){
33         cout << "constructor of Liger" << endl;
34     }
35 };
36 
37 int main()
38 {
39     Liger anil;
40     anil.walk();
41     return 0;
42 }