title: c++ matrix overloading not working
body: <p>Cannot figure out what's happening.
  Why is operator overloading not working in this code ?</p>
  
  <pre><code>class matrix
  {
  protected:
  
      int matrix1[5][5],matrix2[5][5],resultmatrix[5][5];
      int m,n,p,q,i,j,k,l;
      public: matrix()
          {
          }
          void getdata();
          //double determinat;
          matrix operator  +(matrix);
          void showmatrix();
  };
  
      void matrix::getdata()
      {
      cout&lt;&lt;"\n"&lt;&lt;"enter no of rows for matrix1 : "&lt;&lt;"\n";
      cin&gt;&gt;m;
      cout&lt;&lt;"\n"&lt;&lt;"enter no of columns for matrix1 : "&lt;&lt;"\n";
      cin&gt;&gt;n;
      cout&lt;&lt;"\n"&lt;&lt;"enter no of rows for matrix2 : "&lt;&lt;"\n";
      cin&gt;&gt;p;
      cout&lt;&lt;"\n"&lt;&lt;"enter no of columns for matrix2 : "&lt;&lt;"\n";
      cin&gt;&gt;q;
  
      if(m==p&amp;&amp;n==q)
      {
          cout&lt;&lt;"\n"&lt;&lt;"matrixes can be added"&lt;&lt;"\n";
      }
      else
      {
      cout&lt;&lt;"\n"&lt;&lt;"matrixes can not be added"&lt;&lt;"\n";
      exit(0);
      }
  
      cout&lt;&lt;'\n'&lt;&lt;"please provide for matrix1: "&lt;&lt;"\n";
  
      for( i=0;i&lt;m;i++)
      {
          for(j=0;j&lt;n;j++)
          {
              cin&gt;&gt;matrix1[i][j];
          }
      }
  
      cout&lt;&lt;'\n'&lt;&lt;"please provide for matrix2: "&lt;&lt;"\n";
  
      for(int i=0;i&lt;m;i++)
      {
          for(int j=0;j&lt;n;j++)
          {
              cin&gt;&gt;matrix2[i][j];
          }
      }
  
    }
  
      matrix matrix::operator+( matrix matrixobj)
  </code></pre>
  
  <p>{
      matrix tempmatrixobj;</p>
  
  <pre><code>for(i=0;i&lt;m;i++)
  {
      for(j=0;j&lt;n;j++)
      {
          matrixobj.resultmatrix[i][j]=
         tempmatrixobj.matrix1[i][j]+tempmatrixobj.matrix2[i][j];
      }
  }
  return matrixobj;
  //return tempmatrixobj;
  </code></pre>
  
  <p>//  return *this;</p>
  
  <p>}</p>
  
  <pre><code>    void matrix::showmatrix()
       {
        for(i=0;i&lt;m;i++)
        {
          for(j=0;j&lt;n;j++)
          {
              cout&lt;&lt;resultmatrix[i][j]&lt;&lt;"\t";
          }
          cout&lt;&lt;"\n";
        }
  
     }
  
       int main()
       {
         matrix matrixobj1,matrixobj2;
          matrix resultmatrix;
  
         matrixobj1.getdata();
  
         matrixobj2.getdata();
  
      resultmatrix=matrixobj1+matrixobj2;
  
      resultmatrix.showmatrix();
  
      return 0;
    }
  </code></pre>
  
  <p>Addition part of the code not working at all and it takes the values twice for each matrix and thens outputs some memory addresses only.</p>
  
  <p>output:</p>
  
  <p>enter no of rows for matrix1 :<br>
  3</p>
  
  <p>enter no of columns for matrix1 :<br>
  3</p>
  
  <p>enter no of rows for matrix2 :<br>
  3</p>
  
  <p>enter no of columns for matrix2 :<br>
  3</p>
  
  <p>matrixes can be added  </p>
  
  <p>please provide for matrix1:<br>
  1 1 1<br>
  1 1 1<br>
  1 1 1  </p>
  
  <p>please provide for matrix2:<br>
  1 1 1<br>
  1 1 1<br>
  1 1 1  </p>
  
  <p>enter no of rows for matrix1 :<br>
  2</p>
  
  <p>enter no of columns for matrix1 :<br>
  2</p>
  
  <p>enter no of rows for matrix2 :<br>
  2</p>
  
  <p>enter no of columns for matrix2 :<br>
  2</p>
  
  <p>matrixes can be added  </p>
  
  <p>please provide for matrix1:<br>
  1 1<br>
  1 1  </p>
  
  <p>please provide for matrix2:<br>
  1 1<br>
  1 1<br>
     -1717986920     -1717986920<br>
     -1717986920     -1717986920<br>
     Press any key to continue . . .</p>