#include using namespace std; /** * define a generic matrix class */ template class Matrix { public: Matrix(int x = default_x, int y = default_y); Matrix(const Matrix& src); ~Matrix(); // overloaded operators on class Matrix& operator=(const Matrix& rhs); template friend ostream& operator<<(ostream& ostr, const Matrix& mtx); template friend Matrix operator*(const Matrix& a, const Matrix& b); int get_x_size() const { return x_size;} int get_y_size() const { return y_size;} T get_element(int x, int y) const; void set_element(int x, int y, T elem); // constant elements static const int default_x = 3; static const int default_y = 3; protected: T** cells; int x_size; int y_size; }; template Matrix::Matrix(int x, int y): x_size(x), y_size(y) { cells = new T*[x_size]; for(int i = 0; i < x_size; ++i) { cells[i] = new T[y_size]; memset(cells[i], 0, (y_size * sizeof(T))); } } template Matrix::Matrix(const Matrix& src): x_size(src.x_size), y_size(src.y_size) { cells = new T*[x_size]; for(int i = 0; i < x_size; ++i) { cells[i] = new T[y_size]; memcpy_s(cells[i], (y_size * sizeof(T)), src.cells[i], (y_size * sizeof(T))); //memcpy(cells[i], src.cells[i], y_size * sizeof(T)); // same but less safe than memcpy_s } } template Matrix::~Matrix() { for(int i = 0; i < x_size; ++i) { delete[] cells[i]; } delete[] cells; } template Matrix& Matrix::operator=(const Matrix& rhs) { if(this == &rhs) return (*this); // release old memory for(int i = 0; i < x_size; ++i) { delete[] cells[i]; } delete[] cells; // allocate new memory cells = new T*[rhs.x_size]; for(int i = 0; i < rhs.x_size; ++i) { cells[i] = new T[rhs.y_size]; memset(cells[i], 0, (rhs.y_size * sizeof(T))); } // copy values for(int i = 0; i < rhs.x_size; ++i) { for(int j = 0; j < rhs.y_size; ++j) { cells[i][j] = rhs.cells[i][j]; } } return *this; } // friend functions template ostream& operator<<(ostream& ostr, const Matrix& mtx) { for(int i = 0; i < mtx.x_size; ++i) { for(int j = 0; j < mtx.y_size; ++j) { ostr << mtx.cells[j][i] << ", "; } ostr << "\n"; } ostr << "\n"; return ostr; } template Matrix operator*(const Matrix& a, const Matrix& b) { Matrix result(a.x_size, b.y_size); for(int i = 0; i < a.x_size; ++i) { for(int j = 0; j < a.x_size; ++j) { for(int k = 0; k < a.x_size; ++k) { result.cells[i][j] += (a.cells[k][j] * b.cells[i][k]); } } } return result; } template T Matrix::get_element(int x, int y) const { return (cells[x][y]); } template void Matrix::set_element(int x, int y, T elem) { cells[x][y] = elem; } int main() { Matrix mtx1; mtx1.set_element(0,0,1); mtx1.set_element(1,1,4); mtx1.set_element(2,2,6); cout << "matrix 1:\n" << mtx1; Matrix mtx2; mtx2.set_element(0,0,1); mtx2.set_element(1,1,4); mtx2.set_element(2,2,2); cout << "matrix 2:\n" << mtx2; Matrix mtx3; mtx3 = mtx2; cout << "matrix 3 assigned from matrix 2:\n" << mtx3; Matrix product = (mtx1 * mtx3); cout << "matrix product assigned from mtx1 * mtx3:\n" << product; int x; cin >> x; return 0; }