template class matrix { public: unsigned int rows, cols; T* contents; matrix(unsigned int, unsigned int); matrix(const matrix&); matrix(unsigned int, unsigned int, T*); matrix operator*(const matrix&) const; matrix operator+(const matrix&) const; ~matrix(); T* operator[](unsigned int) const; const matrix& operator=(const matrix&); }; template const matrix& matrix::operator=(const matrix& m) { rows = m.rows; cols = m.cols; delete [] contents; contents = new T[rows*cols]; for (unsigned int i = 0; i T* matrix::operator[](unsigned int i) const { return &contents[i*cols]; } template matrix::matrix(unsigned int r, unsigned int c) : rows(r), cols(c), contents(new T[r*c]) { for (unsigned int i = 0; i < r*c; i++) contents[i] = 0; } template matrix::matrix(const matrix& m) : rows(m.rows), cols(m.cols), contents(new T[m.rows*m.cols]) { for (unsigned int i = 0; i < m.rows*m.cols; i++) contents[i] = m.contents[i]; } template matrix::matrix(unsigned int r, unsigned int c, T* t) : rows(r), cols(c), contents(t) { } template matrix::~matrix() { delete [] contents; } template matrix matrix::operator+(const matrix& m) const { //Assume dimensions match, because we're using C++ T* out = new T[rows*cols]; for (unsigned int i = 0; i < rows*cols; i++) out[i] = contents[i] + m.contents[i]; return matrix(rows, cols, out); } template matrix matrix::operator*(const matrix& m) const { //Assume dimensions match, because we're using C++ //We're using row-major order matrix out(rows, m.cols); for (unsigned int i = 0; i < rows; i++) { for (unsigned int j = 0; j < m.cols; j++) { T sum = 0; for (unsigned int x = 0; x < cols; x++) sum += (*this)[i][x] * m[x][j]; out[i][j] = sum; } } return out; }