Oct 24, 2015 at 2:10am UTC
I wonder why there is only public member functions there but no private data members?
And in line 14. why it is not " void setName(string newName)" ?
Here is the code:
#include <iostream>
2.#include <string>
3.using namespace std;
4.
5.const int NUM_ROWS = 8;
6.const int NUM_COLS = 8;
7.
8.// **************** CLASS: PIECE *******************
9.class Piece {
10. string name;
11.public:
12. Piece();
13. string getName();
14. void setName(string);
15.};
16.Piece::Piece() {
17. name = " ";
18.}
19.string Piece::getName() {
20. return name;
21.}
22.void Piece::setName(string newName) {
23. // should do some validation here!
24. name = newName;
25.}
26.
27.// **************** CLASS: CELL *******************
28.class Cell {
29. int row;
30. int col;
31.public:
32. class piece; // <-- Not a good idea in the long run
33.};
34.
35.// **************** CLASS: BOARD *******************
36.class Board {
37. void displayLine();
38.public:
39. Piece board[NUM_ROWS][NUM_COLS]; // <-- Not a good idea in the long run
40. void displayBoard();
41.};
42.
43.void Board::displayLine() {
44. cout << endl;
45. for (int x = 0; x < NUM_COLS; x++) {
46. cout << " | ";
47. }
48. cout << endl;
49. for (int x = 0; x < NUM_COLS; x++) {
50. cout << "----| ";
51. }
52. cout << endl;
53.
54.}
55.
56.void Board::displayBoard() {
57. cout << endl << "CURRENT BOARD:" << endl << endl;
58. for (int r = 0; r < NUM_ROWS; r++) {
59. for (int c = 0; c < NUM_COLS; c++) {
60. cout << " " << board[r][c].getName() << " | ";
61. }
62. displayLine();
63. }
64. cout << endl << endl;
65.}
66.
67.int main() {
68. Board board;
69. board.displayBoard();
70. board.board[0][0].setName("BR");
71. board.displayBoard();
72. return 0;
73.}
Oct 24, 2015 at 2:52am UTC
line 14 for declaration or prototype the signature does not need you to give name to parameter only the type. you said your data is not private.wrong by default all field is private in a class until you set another modifier like public or protected.
Oct 24, 2015 at 6:53pm UTC
So
class Piece {
string name;
public:
Piece();
string getName();
void setName(string);
};
is the same as
class Piece {
private:
string name;
public:
Piece();
string getName();
void setName(string);
};
?