class matrix { protected: // 1d int size; // C++ void f(int x[]) int * data; int position(int i) const { return i; } // 2d int rows, cols; // C++ void f(int x[][10]) int * data; int position(int i, int j) const { return i*cols + j; } // 3d int rows, cols, planes; // C++ void f(int x[][10][7]) int * data; int position(int i, int j, int k) const { return i*rows*cols + j*cols + k; } // etc public: matrix(int r, int c, int p) { cout << " regular constructor\n"; rows = r; cols = c; planes = p; data = new int [r*c*p]; } ~matrix() { cout << " destructor happening\n"; delete [] data; } int numrows() const { return rows; } int numcols() const { return cols; } int numplanes() const { return planes; } bool safe_access(int i, int j, int k) const { if (i<0 || j<0 || i>=rows || j>=cols || k<0 || k>=planes) return false; return true; } int get(int i, int j, int k) const { if (! safe_access(i, j, k)) { cerr << "\narray access out of bounds\n"; exit(1); } return data[position(i, j, k)]; } void put(int i, int j, int k, int value) { if (! safe_access(i, j, k)) { cerr << "\narray access out of bounds\n"; exit(1); } data[position(i, j, k)] = value; } // etc etc etc