Search code examples
c++classobjectparent

c++ do children fit in their parents slots?


In my java class i remember making a barn class that held animal class objects, and my chicken and cow classes (which were children of the animal class) would fit into the barn as if they were their parents.

i was wondering if c++ also allowed this?


Solution

  • Yes it does. The general concept is called the Liskov Substitution Principle.

    One issue to be aware of that arises in C++ but not in Java is slicing: What is object slicing?