Explain the concept of multipath inheritance with the help of suitable programming example.
Multipath Inheritance in C++ is derivation of a class from other derived classes, which are derived from the same base class.This type of inheritance involves other inheritance like multiple, multilevel, hierarchical etc.
Here is programming example:
#include<iostream>
using namespace std;
class ClassA
{
public:
int a;
};
class ClassB : public ClassA
{
public:
int b;
};
class ClassC : public ClassA
{
public:
int c;
};
class ClassD : public ClassB, public ClassC
{
public:
int d;
};
int main()
{
ClassD obj;
obj.ClassB::a = 10;
obj.ClassC::a = 100;
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout << " a from ClassB : " << obj.ClassB::a;
cout << "\n a from ClassC : " << obj.ClassC::a;
cout << "\n b : " << obj.b;
cout << "\n c : " << obj.c;
cout << "\n d : " << obj.d << '\n';
}
Comments
Leave a comment