#include #include "Array2T.h" using namespace std; Array2T::Array2T() { rows = 0; cols = 0; data = nullptr; } Array2T::Array2T(size_t r, size_t c) { rows = r; cols = c; data = new int[rows * cols * sizeof(int)]; } size_t Index(size_t r, size_t c, size_t cols) { return r*cols + c; } void CopyArray(int dest[], const int src[], size_t rows, size_t cols) { size_t r,c; size_t idx; for(r = 0; r < rows; ++r) { for(c = 0; c < cols; ++c) { idx = Index(r,c,cols); dest[idx] = src[idx]; } } } Array2T::Array2T(const Array2T & other) { rows = other.rows; cols = other.cols; data = new int[rows * cols * sizeof(int)]; CopyArray(data, other.data, rows, cols); } Array2T::~Array2T() { delete [] data; } Array2T & Array2T::operator = (const Array2T & other) { if (this != &other) { delete [] data; rows = other.rows; cols = other.cols; data = new int[rows * cols * sizeof(int)]; CopyArray(data, other.data, rows, cols); } return *this; } int Array2T::operator[] (size_t r, size_t c) const { if (r < rows and c < cols) { return data[r*cols + c]; } else { cerr << "Error, Invalid index" << endl; return data[0]; } } int & Array2T::operator[] (size_t r, size_t c) { if (r < rows and c < cols) { return data[r*cols + c]; } else { cerr << "Error, Invalid index" << endl; return data[0]; } } size_t Array2T::Rows() const { return rows; } size_t Array2T::Cols() const { return cols; }