C & C++ Programming - Inheritence Access
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////
//
class CBase
{
public:
CBase(int numCylinders)
{
m_nNumCylinders = numCylinders;
}
void Start() // Starts this CBase
{
cout << "Number of Cylinders = " << m_nNumCylinders;
}
void overhaul_public()
{
cout << __FUNCTION__ << endl;
}
protected:
void overhaul_protected()
{
cout << __FUNCTION__ << endl;
}
private:
void overhaul_private()
{
cout << __FUNCTION__ << endl;
}
private:
int m_nNumCylinders;
};
////////////////////////////////////////////////////////////////////////////////////////////////
//
class CDerived_Priv : private CBase
{
public:
CDerived_Priv()
: CBase(8)
{ } // Initializes this Car with 8 cylinders
// Start this Car by starting its Engine
void Start()
{
CBase::Start();
overhaul_protected();
//overhaul_private(); // Always Private
}
};
////////////////////////////////////////////////////////////////////////////////////////////////
//
class CDerived2 : public CDerived_Priv
{
public:
CDerived2()
{ }
void Start()
{
&emsp ;CDerived_Priv::Start(); // Public functions shall be accessible irrespective of
&emsp ;any inheritance
&emsp ;//overhaul_protected(); // Accessible for class CDerived_Priv : protected CBase
// Not Accessible for class CDerived_Priv : private CBase
}
};
////////////////////////////////////////////////////////////////////////////////////////////////
//
class CDerived_Proc : protected CBase
{
public:
CDerived_Proc()
: CBase(4)
{ } // Initializes this Car with 8 cylinders
// Start this Car by starting its Engine
void Start()
{
CBase::Start();
overhaul_protected();
//overhaul_private(); // Always Private
}
};
class CDerived3 : public CDerived_Proc
{
public:
CDerived3()
{ }
void Start()
{
CDerived_Proc::Start(); // Public functions shall be accessible irrespective of
any inheritance
overhaul_protected(); // Accessible for class CDerived_Proc : protected CBase
// Not Accessible for class CDerived_Proc : private CBase
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TestAccess()
{
CDerived_Priv obj1;
obj1.Start();
//obj1.overhaul_public(); // Can be accessed as CDerived_Priv is privately derived
from CBase hence become private
CDerived_Proc obj2;
obj2.Start();
//obj2.overhaul_public(); // Can be accessed as CDerived_Priv is protected derived
from CBase hence become private
}