#include // --------------------------------------------- MatrixNxN: Dynamic size matrix class MatrixNxN { private: float *A; int N; public: /* ------------------------- Constructor(int, float[]) ------------------------- */ MatrixNxN(int n, float B[]) { int i; A = new float[n*n]; N = n; for (i = 0; i < N*N; i = i + 1) A[i] = B[i]; } friend ostream & operator<<(ostream &, MatrixNxN &); }; // ----------------------------------------------------------------------- ostream & operator<<(ostream & cout, MatrixNxN & M) { int i, j; for (i = 0; i < M.N; i = i + 1) { for (j = 0; j < M.N; j = j + 1) cout << M.A[i*M.N+j] << "\t"; cout << "\n"; } cout << "\n"; return(cout); }; // ----------------------------------------------------------------------- int main(int argc, char *argv[]) { int N = 4; float B[16]; int i, j, k; k = 1; for (i = 0; i < N; i = i + 1) for (j = 0; j < N; j = j + 1) { B[i*N+j] = k; k = k + 1; } MatrixNxN M(N, B); cout << "Matrix M:" << endl; cout << M; // Print Matrix M cout << endl; // We can cascade because we made << return an ostream object: cout << "Second printing of Matrix M:" << endl << M << endl; }