I don't know if your solution will work. |
Yes it will.
We know that every type of building is derived ultimately from the class Building.
PanGalactic has already stated that the vector (or even evil array) must use pointers.
If the classes are implemented correctly (i.e. are well-behaved polymorphic types) then the functionality of derived building types will be accessible via a pointer to their base type (Building*).
Even if the types are not polymorphic, they can still be stored by pointer in a vector which holds pointers to their base type, though it wouldn't work right (without some mucking about.)
Actually, the problem screams "exercise in polymorphism", so this is no doubt exactly what is wanted.
But Sheroz Nazhmudinov does state that "[he needs] to create an array of 20 elements", so maybe he does need to use an (evil) C array. But this will still work correctly it it's an array of Building* pointers.
Andy
PS Would ApartmentBlock be better than Apartment, when it comes to the class hierarchy?