#ifndef MT3Q6_H #define MT3Q6_H /////////////////////////////////////////////////////////////// // MT3Q6b.h - Alternate Matrix // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2012 // /////////////////////////////////////////////////////////////// #include #include template class Row { public: Row(); /////////////////////////////////////////////////////// // compiler generated assignment and copy are correct T& operator[](int i); T operator[](int i) const; private: T Array[N]; }; template Row::Row() { for(int i=0; i T& Row::operator[](int i) { if(i<0 || N<= i) throw std::exception("col index error"); return Array[i]; } template T Row::operator[](int i) const { if(i<0 || N<= i) throw std::exception("row index error"); return Array[i]; } template class Matrix { public: typedef T* row; Matrix(); /////////////////////////////////////////////////////// // compiler generated assignment and copy are correct row& operator[](size_t i); Row operator[](size_t i) const; ////////////////////////////// // alternate indexing scheme T& operator()(int i, int j); T operator()(int i, int j) const; size_t rows() const { return M; } size_t cols() const { return N; } private: T Array[M][N]; T* RowPointer; }; template Matrix::Matrix() { for(size_t i=0; i typename Matrix::row& Matrix::operator[](size_t i) { if(i<0 || M<=i) throw std::exception("row index error"); RowPointer = &Array[i][0]; return RowPointer; } template typename Row Matrix::operator[](size_t i) const { if(i<0 || M<=i) throw std::exception("row index error"); Row row; for(int j=0; j T& Matrix::operator()(int i, int j) { if(i<0 || M<=i || j<0 || N<=j) throw std::exception("index error"); return Array[i][j]; } template T Matrix::operator()(int i, int j) const { if(i<0 || M<=i || j<0 || N<=j) throw std::exception("index error"); return Array[i][j]; } template void Display(const Matrix& m) { for(size_t i=0; i