943,697 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 549
  • C++ RSS
Dec 9th, 2008
0

Dynamic arrays in a class

Expand Post »
I'm working on a program that has a class Board, and two inherited classes that will implement different games, but I'm having problems with setting up the initial Board class. The problem is that I don't know how to make it so the board array in the protected section of the Board.h file takes on the values input in a and b in the main.cpp file.

Here's my main.cpp
#include <iostream>
#include "Board.h"

using namespace std;

int main()
{
    Board Game;
    int a, b;
    cout << "enter a: ";
    cin >> a;
    cout << endl << "enter b: ";
    cin >> b;
    cout << endl;
    Game.set_board(a,b);
    Game.display();

    return 0;
}
my Board.cpp
C++ Syntax (Toggle Plain Text)
  1. #include "Board.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. Board::Board()
  7. {
  8.  
  9. }
  10.  
  11. void Board::display()
  12. {
  13. for (int i = 0; i < width; i++)
  14. {
  15. for (int j = 0; j < height; j++)
  16. {
  17. cout << board[i][j] << " ";
  18. }
  19. cout << endl;
  20. }
  21. }
  22.  
  23. void Board::set_board(int a, int b)
  24. {
  25. width = a;
  26. height = b;
  27.  
  28. int num = 1;
  29.  
  30. for (int i = 0; i < width; i++)
  31. {
  32. for (int j = 0; j < height; j++)
  33. {
  34. board[i][j] = num++;
  35. }
  36. }
  37. }
and my Board.h file
#ifndef BOARD_H_INCLUDED
#define BOARD_H_INCLUDED

class Board
{
    public:
    Board();
    void display();
    void set_board(int, int);

    protected:
    int height, width;
    int board[4][4];
};

#endif // BOARD_H_INCLUDED
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chasee is offline Offline
4 posts
since Sep 2008
Dec 9th, 2008
0

Re: Dynamic arrays in a class

Instead of: int board[4][4];
Replace with: int** board;
Now we can dynamically allocate space for the 2D board array.

In set_board(), we need to do the dynamic allocation like so:
C++ Syntax (Toggle Plain Text)
  1. // First allocate for the first dimension
  2. board = new int*[height];
  3.  
  4. // Now allocate for the second dimension
  5. for( int i = 0; i < height; i++ ) {
  6. board[i] = new int[width];
  7. }
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: opening user specified file
Next Thread in C++ Forum Timeline: Determining the number of unique words in a .txt file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC