As JohnSmith pointed out, you can't override data members, just member functions. Since ParkingLotBuilding
contains ParkingLot
values, and not ParkingLot
pointers or references, they can't be used polymorphically, even in DerivedParkingLot
. (That's just how C++ works: only pointers and references can have a dynamic type.)
That means that if you can't change the ParkingLotBuilding
class (or the CheckBuilding
function), then you're stuck. There is no deriving you can do that will get the CheckBuilding
function to operate on a DerivedParkingLot
object.
The moral of this story is that classes must be designed for inheritance from the beginning.