Is this the expected behavior? How can I call the Print () function to get the expected result?
Yes, this is the expected behavior.
The problem is that standard containers, including vector , have semantics of values: they store copies of the objects you pass to push_back() . On the other hand, polymorphism is based on referential semantics — links or pointers are required for proper operation.
What happens in your case is that your CPolygon object gets a cut that you don't need. You should store pointers (possibly smart pointers) in your vector, not objects like CPolygon .
This is how you should rewrite your main() function:
#include <memory> // For std::shared_ptr int main() { std::vector< std::shared_ptr<CPolygon> > polygons; polygons.push_back( std::make_shared<CRectangle>() ); polygons.push_back( std::make_shared<CTriangle>() ); for (auto it = polygons.begin() ; it != polygons.end(); ++it) { (*it)->Print(); } return 0; }
Here is a living example .
source share