#include <stdlib.h>
#include <iostream.h>
template <class itemType>
class matrix
{
public:
// constructors/destructor
matrix(); // default size 0 x 0
matrix(int rows, int cols); // size rows x cols
matrix(int rows, int cols, const itemType & fillValue); // set fill value
matrix(const matrix & mat); // copy constructor
~matrix(); // destructor
// operator overloads
matrix & operator + (matrix & rhs); // adds two matrices
matrix & operator - (matrix & rhs); // subtracts two matrices
matrix & operator * (matrix & rhs); // multiplies two matrices
matrix & operator = (matrix & rhs);
vector <itemType> & operator [] (int k); // range-checked indexing
// member functions
int numrows() const; // number of rows
int numcols() const; // number of columns
void resize( int newRows, int newCols ); // resizes matrix to newRows x newCols
// (can result in losing values)
private:
int myRows; // # of rows (capacity)
int myCols; // # of cols (capacity)
vector < vector <itemType> > myMatrix; // the matrix of items
};
template <class itemType>
matrix<itemType> &
matrix<itemType>::operation + (matrix<itemType> first, matrix<itemType> second)
{
int j, k;
matrix <itemtype> answer(first.row, first.col);
if(first.col == second.col && first.row == second.row)
{
for(j = 0; j < myRows; j++)
{
for(k = 0; k < myCol; k++)
{
answer[j][k]=first[j][k] + second[j][k];
}
}
}
return answer;
}
template <class itemType>
matrix<itemType> &
matrix<itemType>::operation - (matrix<itemType> first, matrix<itemType> second)
{
int j, k;
matrix <itemType> answer(first.row, first.col);
if(first.col == second.col && first.row == second.row)
{
for(j = 0; j < myRows; j++)
{
for(k = 0; k < myCol; k++)
{
answer[j][k]=first[j][k] - second[j][k];
}
}
}
return answer;
}
template <class itemType>
matrix<itemType> &
matrix<itemType>::operation * (matrix<itemType> first, matrix<itemType> second)
{
int j, k, l;
matrix <itemtype> answer(first.myRow, first.myCol, 0);
if(first.myCol == second.myRow && first.myRow == second.myCol)
{
for(j = 0; j < first.myRows; j++)
{
for(k = 0; k < second.myCol; k++)
{
for(l = 0; l < first.myCol; l++)
{
answer[j][k] += first[j][l] * second[l][k];
}
}
}
}
return *answer;
}