#include #include class Animal { public: // Note: using *virtual* does the difference here! virtual void makeSound() { std::cout << "" << std::endl; } }; class Cow : public Animal { public: void makeSound() { std::cout << "Mooh." << std::endl; } }; class Cat : public Animal { public: void makeSound() { std::cout << "Meow?" << std::endl; } }; int main() { Cat cat; Cow cow; // Does nothing: std::vector animals = { cat, cow }; for (auto &a: animals) { a.makeSound(); } // Meow? Mooh. std::vector animalptrs = { &cat, &cow }; for (auto &a: animalptrs) { a->makeSound(); } }