constructor inheritance c++ code example
Example 1: inheritance protected in c++
class base
{
public:
int x;
protected:
int y;
private:
int z;
};
class publicDerived: public base
{
};
class protectedDerived: protected base
{
};
class privateDerived: private base
{
}
Example 2: 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