c++ inheritance problems code example

Example 1: multiple inheritance in c++

#include<iostream> 
using namespace std; 

class A 
{ 
public: 
A() { cout << "A's constructor called" << endl; } 
}; 

class B 
{ 
public: 
B() { cout << "B's constructor called" << endl; } 
}; 

class C: public B, public A // Note the order 
{ 
public: 
C() { cout << "C's constructor called" << endl; } 
}; 

int main() 
{ 
	C c; 
	return 0; 
}

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?

Tags:

Cpp Example