Consider the following source code. I have two classes CBar and CFoo. CFoo inherits from CBar. The output of this source code is
Bar
Foo
Bar
I was expecting
Bar
Foo
Foo
Where did I go wrong? What I was thinking was that since the CFoo object has a Speak function that overrides the CBar speak function. When I call The Speak() function from a CBar function on an object that is CFoo the CFoo Speak function would be executed. But that assumption appears to be wrong.
class CBar
{
public:
void Speak() {
printf(" Bar \n");
}
void DoStuff() {
this->Speak();
}
};
class Cfoo : public CBar
{
public:
void Speak() {
printf(" Foo \n");
}
void DoStuff() {
CBar::DoStuff();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CBar b;
b.Speak();
Cfoo f;
f.Speak();
f.DoStuff();
return 0;
}