/**********************************************************************
Polymorphism in C++ is implemented through virtual functions and inheritance mechanisms.
Polymorphism can be implemented/applied in various styles
**********************************************************************/
class Base
{
protected:
int m_nValue;
public:
Base()
{
m_nValue = 0;
}
virtual void Increment()
{
m_nValue++; // incremented by 1
}
};
class Derived1 : public Base
{
public:
/* virtual */ void Increment()
{
m_nValue += 2; // incremented by 2
}
};
class Derived2 : public Base
{
public:
virtual void Increment()
{
Base::Increment();
m_nValue += 2; // incremented by 2
}
};
class Derived3_1 : public Derived1
{
public:
void Increment()
{
m_nValue += 3;// incremented by 3
// do something specific to Derived3_1 class
}
};
class Derived4_2 : public Derived2
{
public:
void Increment()
{
m_nValue += 3;// incremented by 3
// do something specific to Derived4_2 class
}
};
/****************************************************************
Let's see polymorphism and how functions are called by monitoring
the value of m_nValue at each line
****************************************************************/
void Polymorhism()
{
// CASE 1 :
// pPoly as Base class pointer and create object of
// any class derived at any level.
// Polymorphism is achieved by following code.
// Virtual functions are maintained in a table
// called VFT (virtual function table) for storing the
// function pointers of all derived objects of derived classes
Base* pPoly = new Derived1();
//m_nValue = 0(Initialized in Base())
pPoly->Increment();
// m_nValue = 2 (Only Derived1::Increment() is called)
delete pPoly;
pPoly = new Derived2();
//m_nValue = 0 (Initialized in Base())
pPoly->Increment();
// m_nValue = 3 (Base::Increment() and Derived2::Increment()
// are called). Observe the difference between this call
// and above call (Dervied1::Increment())
delete pPoly;
pPoly = new Derived3_1();
// m_nValue = 0(Initialized in Base())
pPoly->Increment();
// m_nValue = 3 (Only Derived3_1::Increment() is called)
// Derived3_1::Increment() is called though
// it is not virtual in its immediate parent class Derived1
delete pPoly;
pPoly = new Derived4_2();
// m_nValue = 0 (Initialized in Base())
pPoly->Increment();
// m_nValue = 3 (Only Derived4_2::Increment() is called)
delete pPoly;
// CASE 2 :
// This is also a polymorphism, but with Derived1 class pointer
Derived1* pPoly1 = new Derived3_1();
// m_nValue = 0 (Initialized in Base())
pPoly1->Increment();
// m_nValue = 3 (Only Derived3_1::Increment() is called)
// Observe that it is called though it's not declared as vitual
// in its immediate parent. It happens //because, VFT is maintained
// in super most class object and Derived3_1::Increment()
// function pointer is stoed.
delete pPoly1;
// CASE 3 :
// This is also a polymorphism, but with Derived3 class pointer
Derived2* pPoly2 = new Derived4_2();
// m_nValue = 0 (Initialized in Base())
pPoly2->Increment();
// m_nValue = 3 (Only Derived4_2::Increment() is called)
delete pPoly2;
}
No comments:
Post a Comment