Here is a sample program to show what I mean:
// Base.h
#ifndef Base_incl
#define Base_incl
class Derived; // forward declaration - required because Method2()
// of the Base class references the Derived class
class Base
{
public:
Base(); // constructor
virtual void Method1(Base &);
virtual void Method2(Base &);
virtual void Method3(Derived &); //it is unusual to have a
// derived class object as a parameter for a base
// class method, but as you see, it can be done.
protected:
int mBase;
};
#endif
// Derived.h
#ifndef Derived_incl
#define Derived_incl
#include "Base.h"
class Derived: public Base
{
public:
Derived(); // constructor
virtual void Method2(Base &); // this method
// overrides Base class Method2()
virtual void Method4(Base &); // a new method
// which is not defined for the Base class
private:
int mDerived;
};
#endif
// Base.cpp
#include "Base.h"
#include "Derived.h"
#include <iostream>
using std::cout;
using std::endl;
Base::Base()
{
this->mBase = 2;
}
void Base::Method1(Base & B_Param)
{
cout << (B_Param.mBase) * (this->mBase) << endl;
}
void Base::Method2(Base & B_Param)
{
cout << this->mBase << endl;
}
void Base::Method3(Derived & D_Param)
{
Base & BRef = static_cast<Base &>(D_Param); // This cast creates
// a Base class reference to the D_Param object.
cout << BRef.mBase << endl;
}
//Derived.cpp
#include "Derived.h"
#include <iostream>
using std::cout;
using std::endl;
Derived:

erived()
{
this->mBase = 3;
this->mDerived = 5;
}
void Derived::Method2(Base & B_Param)
{
cout << this->mDerived << endl;
}
void Derived::Method4(Base & B_Param)
{
Derived * DPtr = dynamic_cast <Derived *> (&B_Param); // this cast
// creates a Derived class pointer to B_Param, if and only if,
// B_Param actually is Derived class object, otherwise the
// pointer will be set to 0
if (DPtr != 0) // if B_Param is actually a Derived class object
cout << DPtr->mDerived << endl;
else
cout << B_Param.mBase << endl;
}
If I compile Derived.cpp I get the following errors:
1>c:\documents and settings\blangela\desktop\polymorphismtest
\derived.cpp(27) : error C2248: 'Base::mBase' : cannot access
protected member declared in class 'Base'
1> c:\documents and settings\blangela\desktop\polymorphismtest
\base.h(17) : see declaration of 'Base::mBase'
1> c:\documents and settings\blangela\desktop\polymorphismtest
\base.h(7) : see declaration of 'Base'