title: Matrix Pointer C++
body: <p>This code supposedly creates a 2-d array dynamically. But can't understand head or tail of it. If only someone could explain what this code does step by step, it would be a great help. Help appreciated!    </p>
  
  <pre><code>#include&lt;iostream.h&gt;
  
  class matrix{
       int **p;
       int d1, d2;
  
   public:
       matrix(int x, int y);
       void get_element(int i, int j, int value){ p[i][j]=value; }
       int &amp;put_element(int i,int j) { return p[i][j]; }
  };
  
  matrix::matrix(int x, int y){      
      d1 = x;
      d2 = y;
      p = new int *[d1];
      for(int i = 0; i &lt; d1; i++) p[i] = new int[d2];
  }
  
  int main(){
      int m, n;
      cout &lt;&lt; "enter size of matrix:";
      cin &gt;&gt; m &gt;&gt; n;
      matrix A(m, n);
  
      cout &lt;&lt; "enter matrix elements row by row\n";
      int i, j, value;
  
      for(i = 0; i &lt; m; i++)
          for(j = 0; j &lt; n; j++){
              cin &gt;&gt; value;
              A.get_element(i, j, value);
          }
          cout &lt;&lt; "\n";
          cout &lt;&lt; "A.put_element(1, 2);
  
      return 0;
  };
  </code></pre>