What you see here is called "slicing" . Selecting an object of a derived class from the base class "cuts off" everything that is not in the base class.
In C ++, virtual functions only work correctly for pointers or references. For your example to work correctly, you must do the following:
DerClass myClass; ((BaseClass *) &myClass)->Method1();
Or you could do
BaseClass *pBase = new DerClass; pBase->Method1();
BaseClass *pBase = new DerClass; pBase->Method1();
Dima source share