So confused with classes

Okay so I am new to classes and I am beyond confused. I have to create a battleship program using this header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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
  friend bool compare(location, location); 

private:
  static const int 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:
  static const int 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?
                                   
}
1
2
3
4
5
6
7
void fleet::deployFleet(void){
	int i=0;
	while(i<fleetSize){
		ships[i].loc.pick(); 
	}                           
                                   
}


But to be able to do this, loc can't be private in Ship

EDIT: Another thing to be aware of is that this way two ships could possibly end up in the same location.
Last edited on
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.
Can anyone help?
1
2
3
4
5
6
7
8
void fleet::deployFleet(void){
    for ( unsigned i=0; i<fleetsize; ++i )
    {
        location loc ;
        loc.pick() ;
        ships[i].setLocation(loc) ;
    }
}
How about making fleet a friend class of location
Topic archived. No new replies allowed.