Creating an array using class in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yue219
    New Member
    • Oct 2007
    • 7

    Creating an array using class in C++

    Im having trouble with this one. I need to create a grid using class, but not sure how to write the code. Please i just need hints =]


    Code:
       
            //array size for Template
            const int R = 4; 
            const int C = 3;
    
        //This is what the player sees upon starting the game.
        char Template[R][C];
        Template[0][0] = 'X';
        Template[0][1] = 'X';
        Template[0][2] = 'X';
        Template[1][0] = 'X';
        Template[1][1] = 'X';
        Template[1][2] = 'X';
        Template[2][0] = 'X';
        Template[2][1] = 'X';
        Template[2][2] = 'X';
        Template[3][0] = 'X';
        Template[3][1] = 'X';
        Template[3][2] = 'X';
  • sanYAua007
    New Member
    • Oct 2007
    • 6

    #2
    Originally posted by yue219
    Im having trouble with this one. I need to create a grid using class, but not sure how to write the code. Please i just need hints =]
    Hello yue219!

    You need something like this.
    Code:
    #include <iostream>
    //-------------------------------------------------------   
    class cGrid
    {
      public:
         cGrid(int _r, int _c)   //Constructor
           :R(_r),C(_c)//Initialize dimensions
         {
            Template=new char*[R];
            for (int i=0; i<R; ++i)
            {
              Template[i]=new char[C];
              for (int j=0; j<C; ++j)
                  Template[i][j]='X';
            } //Initializing array
         }
         ~cGrid() //Destructor
         {
            for (int i=0; i<R; ++i) delete[] Template[i];
            delete []Template; //Release heap memory   
        }
         int R;
         int C;
         char** Template;
    };
    //---------------------------------------------------------------------------
    int main(int argc, char* argv[])
    {
        cGrid myGrid(4,3);
        //You have an instance of cGrid, that contains three fields
        //int R - number of rows
        //int C- number of columns
        //char** Template - 2d array (R*C) each element of which equals 'X'
        //-----------------------------------------------------------------
        //To reach Template's elements use this code
        //myGrid.Template[i][j]
       //Code compiled in C++ Builder.
    }
    That's all. Good luck!

    Comment

    • yue219
      New Member
      • Oct 2007
      • 7

      #3
      Originally posted by sanYAua007
      Hello yue219!

      You need something like this.
      Code:
      #include <iostream>
      //-------------------------------------------------------   
      class cGrid
      {
        public:
           cGrid(int _r, int _c)   //Constructor
             :R(_r),C(_c)//Initialize dimensions
           {
              Template=new char*[R];
              for (int i=0; i<R; ++i)
              {
                Template[i]=new char[C];
                for (int j=0; j<C; ++j)
                    Template[i][j]='X';
              } //Initializing array
           }
           ~cGrid() //Destructor
           {
              for (int i=0; i<R; ++i) delete[] Template[i];
              delete []Template; //Release heap memory   
          }
           int R;
           int C;
           char** Template;
      };
      //---------------------------------------------------------------------------
      int main(int argc, char* argv[])
      {
          cGrid myGrid(4,3);
          //You have an instance of cGrid, that contains three fields
          //int R - number of rows
          //int C- number of columns
          //char** Template - 2d array (R*C) each element of which equals 'X'
          //-----------------------------------------------------------------
          //To reach Template's elements use this code
          //myGrid.Template[i][j]
         //Code compiled in C++ Builder.
      }
      That's all. Good luck!
      Ohh thank you mucho =]

      Comment

      Working...