problems in inheritance in c++ code example
Example 1: ambiguity in inheritance c++
#include<iostream.h>
#include<conio.h>
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;
};
void main()
{
ClassD obj;
obj.ClassB::a = 10;
obj.ClassC::a = 100;
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< "\n 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;
}
Output :
A from ClassB : 10
A from ClassC : 100
B : 20
C : 30
D : 40
Example 2: Diamond inheritance
The "diamond problem" (sometimes referred to as the "Deadly Diamond of Death") is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C
If there is a method in A that B and C have overridden, and D does not override it, then which class of the method does D inherit: that of B, or that of C?