Subclass interact with other subclass? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Subclass interact with other subclass?

Lets say I have a superclass RoomObjects. class Player and class Door are subclasses. How is a good way for these two subclasses to interact with each other? Only way I can think of is storing them in a vector of Objects and check them in a third class, Room. Is there no way for a Player object to have a method that takes a Door object as parameter? Like player.interactDoor(&door). That would prevent iteration over a vector. Or is it better to just scratch this and have Player and Door not as subclasses?

16th Dec 2017, 12:42 AM
Coinage
Coinage - avatar
2 Answers
+ 1
Your class of room objects should only contain attributes that are common to other room objects. one of those attributes should probably be the room they belong to. Then you can have the player do something like, room.findDoor(doorName).walkThrough() The class for the room should have the list of objects in the room like doors, windows, bed, table, etc... The find door method should iterate through that list to find a door with that identification value. Hope that makes sense.
17th Dec 2017, 11:44 PM
Kevin Gilkey-Graham
Kevin Gilkey-Graham - avatar
0
I will try to make it like that. Thank you!
18th Dec 2017, 1:03 PM
Coinage
Coinage - avatar