I am implementing the game Monopoly in C++. I am dealing with an abstract superclass BoardSquare
(whose only property is the square name), with various subclasses depending on the type of game square (whether it's a property, chance, etc.)
Now since I want to represent the board as an array and the squares differ in their types, I have declared an array of BoardSquare
pointers:
GameSquare** board = new GameSquare*[40];
and continued to populate each entry depending on the type:
board[1] = new Property("Old Kent Road", 0);
board[2] = new CommunityChest();
board[3] = new Property("Whitechapel Road", 0);
and so on.
Now Property
, for example, has specific methods such as getPrice()
. But running
board[1]->getPrice()
gives the error
‘class GameSquare’ has no member named ‘getPrice’
which tells me that the pointer is still being treated as a GameSquare. Is there a way around this? Typecasting perhaps? or a more elegant implementation altogether?