You have declared a member variable
enum TYPE type;
in both class B
and A
but your B
constructor never initialises its own variable with that name - it just passes it to the A
constructor which initialises A::type
B::B(enum TYPE kind) :
A(kind)
{
}
Thus the B
member is never initialised. It is not clear why you have added a variable of the same name and type again in the derived class B
but my guess is that perhaps you did not intend this and rather just want a single variable of this type in the base class A
When you have this situation with same variable name and type in both a derived and base class, the derived class member shadows the base class member - this is why accessing type
from your Number::print()
method refers to the B
member rather than the A
member. Obviously then casting this
to A*
accesses the A::type
member.