#include <iostream.h>


// --------------------------------------------- MatrixNxN: Dynamic size matrix
class MatrixNxN
{
public:
   float *A;
   int    N;

   /* ------------------------------------------
      Constructor(int)
      ------------------------------------------ */
   MatrixNxN(int n)
   {
      cout << "Allocating an nxn float matrix, with n = " << n << endl << endl;
      A = new float[n*n];
      N = n;
   }

   void operator=(MatrixNxN & M)
   {
      int i, j;

      cout << "Calling MatrixNxN::operator=(MatrixNxN) " << endl << endl;

      if ( M.N != N )
      {
	 cout << "**Unequal size matrices cannot be copied..." << endl << endl;
	 return;
      }

      for (i = 0; i < N; i=i+1)
         for (j = 0; j < N; j=j+1)
            A[i*N+j] = M.A[i*N+j];
   }


   /* ------------------------
      Print(): print matrix
      ------------------------ */
   void Print()
   {
      int i, j;

      for (i = 0; i < N; i = i + 1)
      {
         for (j = 0; j < N; j = j + 1)
            cout << A[i*N+j] << "\t";
         cout << "\n";
      }
      cout << "\n";
   };
};
// -----------------------------------------------------------------------




int main(int argc, char *argv[])
{
   int N = 4;
   MatrixNxN M(N), Q(N);

   int i, j, k;


   k = 1;
   for (i = 0; i < N; i = i + 1)
      for (j = 0; j < N; j = j + 1)
      {
         M.A[i*N+j] = k;
         k = k + 1;
      }

   cout << "Matrix M:" << endl;
   M.Print();
   cout << endl;

   Q = M;

   // ----------------------- See what happens when we change Q.A[0][0]
   Q.A[0] = 1234;

   cout << "After setting Q.A[0][0] = 1234: " << endl << endl;

   cout << "Matrix M:" << endl;
   M.Print();
   cout << endl;
}