Inheritance

Im making a battleships game, where I need to place ships of different lengths (Aircraft Carrier = length 5, Battleship length = 4, Destroyer length = 3, Submarine length = 3, Patrol Boat length = 2) on a 10x10 array specified by a letter (a>j) on the x-coordinate and (1>10) on the y-coordinate. I have a class "Ships", where I have classes for each type of ship derived from the class "Ships" (Aircraft Carrier, Battleship, Destroyer, Submarine, Patrol Boat) they do not have any code in them yet. I made a class for the gameplay of the game:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Gameplay
{
public:
	Gameplay();

	void playGame();
	
private:
	char _yourBoard [10][10]; //Multidimensional array

	char _getXCoord();
	int _getYCoord();

	bool placeShip(char x, int y);
	
	//Clears the board
	void _clearBoard();

	//Prints the board
	void _printBoard();
};


This is my array:

1
2
3
4
5
6
7
8
9
10
11
void Gameplay::_printBoard()
{
	cout << endl;
	cout << "---------------------------------------------\n";
	cout << "|   | a | b | c | d | e | f | g | h | i | j |\n";
	for (int i = 0; i < 10; i++){
		cout << "---------------------------------------------\n";
		cout << "| " << i + 1 << " | " << _yourBoard[i][0] << " | " << _yourBoard[i][1] << " | " << _yourBoard[i][2] << " | " << _yourBoard[i][3] << " | " << _yourBoard[i][4] << " | " << _yourBoard[i][5] << " | " << _yourBoard[i][6] << " | " << _yourBoard[i][7] << " | " << _yourBoard[i][8] << " | " << _yourBoard[i][9] << " | \n";
	}
	cout << "---------------------------------------------\n";
}


Here's my class "Ships":

1
2
3
4
5
6
7
8
9
10
11
12
13
class Ships
{
public:


protected:
	int shipLength;
	char shipIdentifier;
	string _shipName;

private:

};


my question is how do I pass the name of each ship into the char _getXCoord(); int _getYCoord(); functions, the ship identifier(wich is a diiferent symbol to distinguish between the ships e.g: @,#,$,%) and the ship length?
it's been long time since i did inheritance and little practice i did after studying it. but anyway, if i am getting you query right, since each type of ship inherits from "Ships" but again "Ships" should have their own gameplay. so you inherit the ship class from "GamePlay", so each type of ship would have access to char _getXCoord();and int _getYCoord(); function.you would need to change those function to 'protected' type though.
Topic archived. No new replies allowed.