#Subclass object into function that takes superclass object

1 messages · Page 1 of 1 (latest)

scenic ruin
#

If I have a function that takes in a cake, would I be able to pass a chocolate_cake into the function if chocolate_cake was a subclass of cake?

quick iris
#

Yes, but it will 'slice' the chocolate_cake object into just a cake, and call the cake's methods instead. You need to pass a reference to that chocolate_cake to use polymorphism, I believe

clear fossil
#

correct

#

derived classes are almost always bigger than their base classes. So if you have a "box" the size of the base class, then trying to put a derived class into that box "slices off" the extra information from the derived class

#

so you use references/pointers to get around this