C++20 sections · 1024 units
Open in Course

Practice: Animal Hierarchy

Build polymorphic system

Create a base class Animal with name and age members plus a virtual speak() function. get Dog and Cat classes that override speak() to print appropriate sounds like bark and meow. Make a vector of Animal pointers containing both Dog and Cat objects.

Loop through and call speak() on each. Verify that the correct sound prints for each animal type through polymorphism. Add a virtual destructor to Animal. Create objects with new, store them in the vector, then delete each pointer.

Confirm no memory leaks occur by using a memory checker.