#ifndef MATRIX_H
#define MATRIX_H
#include <string>
#include <stdexcept>
class matrixBase
{
public:
struct matrixError : public std::logic_error
{
matrixError(const std::string& r) : std::logic_error(r) { }
};
struct indexError : public matrixError
{
indexError(int i, int j) : matrixError("Bad index"), row(i), col(j) { }
int row;
int col;
};
matrixBase(int i, int j) : x(i), y(j) { }
int rows() const { return x; }
int cols() const { return y; }
protected:
int x;
int y;
void check(int i, int j) const throw(indexError )
{
if ( 0 > i || i >= x || 0 > j || j >= y )
throw indexError(i,j);
}
};
template <class T>
class matrix : public matrixBase
{
public:
matrix(int i, int j );
matrix(const matrix &other);
~matrix();
matrix operator=( const matrix &other);
T& at( int i, int j) throw(indexError);
T at( int i, int j) const throw(indexError);
T& operator()( int i, int j);
T operator()( int i, int j) const;
matrix operator+=( const matrix &other);
private:
T *v;
void copy( const matrix &other);
};
#endif