1

I know what the diamond problem is I do understand in Java and C++ too. but the problem is that I didn't understand is what's actually the virtual keyword does to solve the problem. I did not find that what exactly is?

If anyone can give me the short explain with example that is something appreciated.

In below code I use A, B, C, D as well to understand easily.

#include<iostream>
using namespace std;
class PersonA {
public:
    PersonA(int x)  { cout << "PersonA::PersonA(int ) called" << endl;   }
    PersonA()     { cout << "PersonA::PersonA() called" << endl;   }
};
  
class FacultyB : virtual public PersonA {
public:
    FacultyB(int x):PersonA(x)   {
       cout<<"FacultyB::FacultyB(int ) called"<< endl;
    }
};
  
class StudentC : virtual public PersonA {
public:
    StudentC(int x):PersonA(x) {
        cout<<"StudentC::StudentC(int ) called"<< endl;
    }
};
  
class TAD : public FacultyB, public StudentC  {
public:
    TAD(int x):StudentC(x), FacultyB(x)   {
        cout<<"TAD::TAD(int ) called"<< endl;
    }
};
  
int main()  {
    TAD ta1(30);
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Irtiza Ali
  • 11
  • 2
  • In theory, `virtual` inheritance is a cool feature of C++, that every student learns. In practice, I have never used it in more than 20 years of C++ programming... – prapin Jan 28 '22 at 13:49

0 Answers0