#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// coordinates (location) of the ship and shots
class location{
public:
location(void); // void constructor, assigns -1 to X
void pick(void); // picks a random location
void fire(void); // asks the user to input the
// coordinates of the next shot
void print(void) const; // prints location in format "a1"
// returns true if the two locations match
friendbool compare(location, location);
private:
staticconstint fieldSize=5; // the field (ocean) is fieldSize X fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
// contains ship's coordinates (location) and whether is was sunk
class ship{
public:
ship(void); // void constructor, sets sunk=false
bool match(location) const; // returns true if this location matches
// the ship's location
bool isSunk(void) const {return(sunk);}; // checks to see if the ship is sunk
void sink(void); // sets "sunk" member variable of the ship to true
void setLocation(location); // deploys the ship at the specified location
void printShip(void) const; // prints location and status of the ship
private:
location loc;
bool sunk;
};
// contains the fleet of the deployed ships
class fleet{
public:
void deployFleet(void); // deploys the ships in random locations
// of the ocean
bool operational(void) const; // returns true if at least
// one ship in the fleet is not sunk
bool isHitNSink(location); // returns true if there was a deployed
// ship at this location (hit) and sinks it
// otherwise returns false (miss)
void printFleet(void) const; // prints out locations of ships in fleet
private:
staticconstint fleetSize=5; // number of battleships
int check(location); // returns index of the element of array ships
// that matches location, returns -1 if
// none match
ship ships[fleetSize]; // battleships of the fleet
};
#endif /* BATTLESHIP_H_ */
Now the method deployFleet has to use the class ship and the class location. So if my code is like this:
1 2 3 4 5 6 7
void fleet::deployFleet(void){
int i=0;
while(i<fleetSize){
ships[i] = <=== how to i call the pick method from
} class location here?
}
Yeah i have another function check() that will make sure there are no same locations. But I am not allowed to make loc public. It has to stay private. I don't understand how that is possible though.