chubbywubba 0 Newbie Poster

I have these two class and I have to make the Dynamic3DArray class a template. I started it but now I am lost and I do not know where to go or what to do. I tried looking online but nothing I find I can seem to understand. Can someone please help?

Dynamic3DArray class

#ifndef dArray_H_
#define dArray_H_
#include "node.h"
 
template <class T>
class Dynamic3DArray{
protected:
     int ROW;
     int COL;
     int DEPTH;
     T*** array;
public:
     Dynamic3DArray();           //Default Constructor
     Dynamic3DArray(int row, int col, int depth);    //Overloaded Constructor
     Dynamic3DArray(const Dynamic3DArray& dArray);     //Copy Constructor
     Dynamic3DArray& operator = (const Dynamic3DArray& dArray); //Operator 
     ~Dynamic3DArray();           //Destructor
 
 //Accessors
     Room* getRoom(int row, int col, int depth);
 //Mutators
     void construct();
     void destroy();
     void resetSize(int row, int col, int depth);
     void print();
     void setDimensions(int row, int col, int depth);
};
 
template <class T>
Dynamic3DArray::Dynamic3DArray(){
     ROW = 0;
     COL = 0;
     DEPTH = 0;
     array = NULL;
}
template <class T>
void Dynamic3DArray::construct(){
     array = new Room** [ROW];
     for (int r = 0; r < ROW; r++){
          array [r] = new Room* [COL];
          for (int c = 0; c < COL; c++)
          array [r][c] = new Room [DEPTH];
     }
}
template <class T>
Dynamic3DArray::Dynamic3DArray(int row, int col, int depth){
 
     this -> ROW = row;
     this -> COL = col;
     this -> DEPTH = depth;
     construct();
 
}
template <class T>
Dynamic3DArray::Dynamic3DArray(const Dynamic3DArray& dArray){
 
     this -> ROW = dArray.ROW;
     this -> COL = dArray.COL;
     this -> DEPTH = dArray.DEPTH;
     construct();
     for (int r = 0; r < ROW; r++){
          for (int c = 0; c < COL; c++){
               for (int d = 0; d < DEPTH; d++)
               array [r][c][d] = dArray.array[r][c][d];
          }
     }
}
template <class T>
void Dynamic3DArray::destroy(){
     if (array == NULL) return; 
     for (int r = 0; r < ROW; r++){
          for (int c = 0; c < COL; c++) {
               delete [] array [r][c];
          }
          delete [] array [r];
     }
     delete [] array;
}
template <class T>
Dynamic3DArray::~Dynamic3DArray(){
     destroy();
}
template <class T>
Dynamic3DArray& Dynamic3DArray::operator = (const Dynamic3DArray& dArray){
 
     destroy();
     this -> ROW = dArray.ROW;
     this -> COL = dArray.COL;
     this -> DEPTH = dArray.DEPTH;
     construct();
     for (int r = 0; r < ROW; r++){
          for (int c = 0; c < COL; c++){
               for (int d = 0; d < DEPTH; d++)
               array [r][c][d] = dArray.array[r][c][d];
          }
     }
     return *this;
}
template <class T>
Room* Dynamic3DArray::getRoom(int row, int col, int depth){
     return &array[row][col][depth];
}
template <class T>
void Dynamic3DArray::resetSize(int row, int col, int depth){
 
     this -> destroy();
     this -> ROW = row;
     this -> COL = col;
     this -> DEPTH = depth;
     construct();
 
}
template <class T>
void Dynamic3DArray::setDimensions(int row, int col, int depth){
     this -> ROW = row;
     this -> COL = col;
     this -> DEPTH = depth;
 
}
template <class T>
void Dynamic3DArray::print(){
      for (int r = 0; r < ROW; r++){
           for (int c = 0; c < COL; c++){
                for (int d = 0; d < DEPTH; d++){
                     array [d][r][c].draw();
                }
                cout <<"\b "<<endl;
           }
           cout <<"\b "<<endl;
       }
       cout << endl;
}
#endif

this is the other class

#include <stdio.h>
#include <time.h>
#ifndef grid_H_
#define grid_H_
 
class Grid:public Dynamic3DArray<Room> {
 
protected:
 
     int curROW;
     int curCOL;
     int curDEPTH;
 
public:
 
 //Managers
     Grid();
     Grid(int r, int c, int d);
     Grid(Grid& grid);
     ~Grid();
     Grid& operator = (Grid& grid);
 
 
 };
 
Grid::Grid():Dynamic3DArray<Room>(){
}
 
Grid::Grid(int r, int c, int d):Dynamic3DArray<Room>(r, c, d){
     curROW = ROW/2;
     curCOL = COL/2;
     curDEPTH = DEPTH/2;
     array[curROW][curCOL][curDEPTH].setActive();
}
 
Grid::Grid(Grid& grid):Dynamic3DArray<Room>(grid){
 
     curROW = grid.curROW;
     curCOL = grid.curCOL;
     curDEPTH = grid.curDEPTH;
}
 
Grid::~Grid(){
}
 
Grid& Grid::operator = (Grid& grid){
 
     destroy();
     this -> ROW = grid.ROW;
     this -> COL = grid.COL;
     this -> DEPTH = grid.DEPTH;
     construct();
     curROW = grid.curROW;
     curCOL = grid.curCOL;
     curDEPTH = grid.curDEPTH;
     for (int r = 0; r < ROW; r++){
          for (int c = 0; c < COL; c++){
               for (int d = 0; d < DEPTH; d++)
               array [r][c][d] = grid.array[r][c][d];
          }
     }
     return *this;
}
 
#endif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.