| | |
Dynamic arrays in a class
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 4
Reputation:
Solved Threads: 0
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
my Board.cpp
and my Board.h 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;
} C++ Syntax (Toggle Plain Text)
#include "Board.h" #include <iostream> using namespace std; Board::Board() { } void Board::display() { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { cout << board[i][j] << " "; } cout << endl; } } void Board::set_board(int a, int b) { width = a; height = b; int num = 1; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { board[i][j] = num++; } } }
#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•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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:
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)
// First allocate for the first dimension board = new int*[height]; // Now allocate for the second dimension for( int i = 0; i < height; i++ ) { board[i] = new int[width]; }
![]() |
Similar Threads
- Vector Base Class (C++)
- Delphi Class memory access violation. Why??? (Pascal and Delphi)
- binary file into class using dynamic arrays (C++)
- Dynamic Mmeory Allocation with 2D Arrays (C++)
- How to pass dynamic arrays? (C)
- Pointers and Dynamic Arrays (C)
- Dynamic arrays inside a class? (C++)
- Help w/ dynamic list funtction definitions (C++)
Other Threads in the C++ Forum
- Previous Thread: opening user specified file
- Next Thread: Determining the number of unique words in a .txt file
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





