Dynamic Array

Hello, I built a 2-D array with a single dynamic array and a dynamic array inside each one of those. But now I want to build a 2D array inside some of those slots.
I know this is some inception level stuff. But any help would be greatly appreciated.

Row.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <cmath>

using namespace std;

class Row
{
private:
    int cols;    // number of columns (array size)
    bool *table; // single dimension array of 0's
public:
    Row();       //Default constructor
    Row(int);    //Constructor with columns parameter
    ~Row();      //Destructor
    int getRows() {return cols;} //Get size of rows
    void setRows(int newRows) {cols = newRows;} // Sets the size of rows
    bool getEl(int y) {return table[y];} // Gets elements in row location
    void setEl(int); // sets the element in the row to true
    void setEl(int,int);
};


Row.cpp
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
#include "Row.h"


Row::Row() //Default constructor
{    
    cols = 9;
    table = new bool[cols];
    
    for(int i = 0; i < cols; i++)
    {
        table[i] = 0;
    }
}

Row::Row(int newRow) //Constructor with columns parameter
{
    cols = newRow;
    table = new bool[cols];
    
    for(int i = 0; i < cols; i++)
    {
        table[i] = 0;
    }
}

void Row::setEl(int y)  // sets the element in the row to true
{ 
    table[y] = 1;
}

void Row::setEl(int y,int value)
{
    table[y] = value;
}


Row::~Row() //Destructor
{
    delete [] table;
}


Cols.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Row.h"

class Cols : public Row
{
private:
    int szCols;         // size of columns
    int szRows;         // size of rows
public:
    Row **level;        // 2-D array to construct where rooms are
    Cols();             // Default Constructor
    Cols(int,int);      //Constructor with rows and columns parameter
    ~Cols();            //Destructor
    int getSzCols() {return szCols;}  // Get Column size
    int getSzRows() {return szRows;}  // Get Rows size
    bool getData(int,int);          // Get the data from the level array
    void setEl(int,int);
};


Cols.cpp
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
#include "Cols.h"

Cols::Cols() //Default Constructor
{
    szCols = 9;
    szRows = 9;
    level = new Row*[szRows];
    
    for(int i = 0; i < szRows; i++)
    {
        level[i] = new Row(szCols);
    }
}

Cols::Cols(int newRows, int newCols) //Constructor with rows and columns parameter
{
    szRows = newRows;
    szCols = newCols;
    
    level = new Row*[szRows];
    
    for(int i = 0; i < szRows; i++)
    {
        level[i] = new Row(szCols);
    }
}
    
bool Cols::getData(int x, int y) // Get the data from the level array
{
    return level[x]->getEl(y);
}

Cols::~Cols()                   //Destructor
{
    for(int i = 0; i < szRows; i++)
    {
        delete level[i];
    }
    delete [] level;
}

Now i can make a 2D array with this concept but how to do i build a 2d inside a 2d?
Some functions are used for my other .h's
Can't figure out what you want.
If I understand correctly, are you trying want to put a row into some columns, and another 2d array into some other columns?
If that's the case, since you have a class for each dimension, you could just create another 2d array class which inherits the Row class.
A little bit terrible, but that is based on your code.
Topic archived. No new replies allowed.