First a general matrix template would have to be created:

C++

template<typename T> class Matrix { public: // default constructor (empty) Matrix() : _rows(0), _cols(0) {}; // constructor with specified size Matrix(int rows, int cols): _rows(rows), _cols(cols), _data(rows* cols) { }; // constructor with initializer_list Matrix(std::initializer_list<std::initializer_list<T>> elements) : _rows(elements.size()), _cols(elements.begin()->size()), _data(_rows* _cols) { int row = 0, col = 0; for (auto row_elem : elements) { col = 0; for (auto val : row_elem) { _data[row * _cols + col] = val; ++col; } ++row; } } // Access functions (with protection) T& operator()(int i, int j) { if (i < 0 || i >= _rows || j < 0 || j >= _cols) { throw std::out_of_range("Matrix index out of range"); } return _data[i * _cols + j]; } const T& operator()(int i, int j) const { if (i < 0 || i >= _rows || j < 0 || j >= _cols) { throw std::out_of_range("Matrix index out of range"); } return _data[i * _cols + j]; } // Getter functions int rows() const { return _rows; } int cols() const { return _cols; } // matrix multiplication operator Matrix<T> operator*(const Matrix<T>& other) const { if (_cols != other._rows) { throw std::invalid_argument("Number of columns of first matrix must be equal to number of rows of second matrix"); } Matrix<T> result(_rows, other._cols); for (int i = 0; i < _rows; i++) { for (int j = 0; j < other._cols; j++) { // TODO: Matrix multiplication } } return result; } // Overload the output operator << as a friend function inside the matrix class. friend std::ostream& operator<<(std::ostream& os, const Matrix<T>& m) { if (m._data.empty() || m._rows == 0 || m._cols == 0) { return os << ""; } for (int i = 0; i < m._rows; ++i) { for (int j = 0; j < m._cols; ++j) { os << m(i, j) << " "; } os << std::endl; } return os; } private: int _rows, _cols; std::vector<T> _data; };

A test program could then look like this:

C++

using namespace std::complex_literals; typedef std::complex<double> MatComplex; // Defining the matrices Matrix<MatComplex> A = { {1, 2i, 3}, {4i, 5, 6} }; Matrix<MatComplex> B = { {7, 8i}, {9i, 10}, {11, 12i} }; Matrix<MatComplex> C; std::cout << "Matrix A:\n" << A << std::endl; std::cout << "Matrix B:\n" << B << std::endl; C = A * B; // multiply the matrices std::cout << "Matrix C = A*B:\n" << C << std::endl;