Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MATRIX_H
- #define MATRIX_H
- #include<iostream>
- using namespace std;
- template<class T>
- class Matrix
- {
- private:
- int m_RowSize;
- int m_ColumnSize;
- T**m_Data;
- public:
- Matrix(int RowSize,int ColumnSize);
- Matrix()
- {
- m_RowSize=0;
- m_ColumnSize=0;
- }
- Matrix(const Matrix<T> &);
- ~Matrix(void)
- {
- for(int i=0;i<m_RowSize;i++)
- delete[] m_Data[i];
- delete [] m_Data;
- }
- void SetElement(int Row,int Column,const T& Value);
- void SetInput();
- T GetElement(int Row ,int Column) const ;
- void GetOutput();
- bool operator!() const;
- T &operator() (int Row,int Column);
- Matrix<T> operator+(const Matrix<T> &);
- const Matrix<T> & operator+=(const Matrix<T> &);
- Matrix<T> operator - (const Matrix<T> &);
- Matrix<T> operator* (const Matrix<T> &);
- void SetSize(int RowSize,int ColumnSize);
- const int& GetColumnSize() const;
- const int & GetRowSize() const;
- };
- template<class T>
- Matrix<T>::Matrix(int RowSize,int ColumnSize)
- :m_RowSize(RowSize),m_ColumnSize(ColumnSize)
- {
- m_Data=new T *[m_RowSize];
- for(int i=0;i<m_RowSize;i++)
- {
- m_Data[i]=new T [m_ColumnSize];
- }
- }
- template<class T>
- Matrix<T>::Matrix(const Matrix<T> & second)
- {
- m_RowSize=second.GetRowSize();
- m_ColumnSize=second.GetColumnSize();
- m_Data=new T *[m_RowSize];
- for(int i=0;i<m_RowSize;i++)
- {
- m_Data[i]=new T [m_ColumnSize];
- }
- for(int i=0;i<m_RowSize;i++)
- for(int j=0;j<m_ColumnSize;j++)
- m_Data[i][j]=second.GetElement(i,j);
- }
- template<class T>
- void Matrix<T>::SetElement(int Row,int Column,const T& Value)
- {
- if(m_RowSize>Row && m_ColumnSize>Column)
- m_Data[Row][Column]=Value;
- }
- template<class T>
- T Matrix<T>::GetElement(int Row, int Column) const
- {if(m_RowSize>Row && m_ColumnSize>Column)
- return m_Data[Row][Column];
- else cerr<<"Wrong input.";
- }
- template<class T>
- void Matrix<T>::SetInput()
- {
- for(int i=0;i != m_RowSize;i++)
- { for (int j=0;j!=m_ColumnSize;j++){
- cout<<"Enter entry:";
- T a;
- cin>>a;
- SetElement(i,j,a);}
- cout<<endl;}
- }
- template<class T>
- void Matrix<T>::GetOutput()
- {
- for(int i=0;i!= m_RowSize;i++)
- { for (int j=0;j!=m_ColumnSize;j++){
- cout<<endl;
- cout<<GetElememt(i,j);}
- cout<<endl;}
- }
- template<class T>
- bool Matrix<T>::operator !() const
- {
- return (m_RowSize==0);
- }
- template<class T>
- T & Matrix<T>::operator ()(int Row,int Column)
- {
- if(m_RowSize>Row && m_ColumnSize>Column)
- return m_Data[Row][Column];
- else cerr<<"Wrong input.";
- }
- template<class T>
- const Matrix<T> & Matrix<T>::operator+=(const Matrix<T> & second)
- {
- if(m_RowSize!=second.GetRowSize() || m_ColumnSize!=second.GetColumnSize())
- cerr<<"Wrong input.";
- for(int i=0;i<m_RowSize;i++)
- for(int j=0;j<m_ColumnSize;j++)
- {
- m_Data[i][j]+=second.GetElement(i,j);
- }
- return *this;
- }
- template<class T>
- Matrix<T> Matrix<T>::operator +(const Matrix<T> & second)
- {
- Matrix<T> Temp(second);
- if(m_RowSize!=second.GetRowSize() || m_ColumnSize!=second.GetColumnSize())
- cerr<<"Wrong input.";
- Temp+=*this;
- return Temp;
- }
- template<class T>
- Matrix<T> Matrix<T>::operator -(const Matrix<T> & second)
- {
- Matrix<T> Temp(second);
- if(m_RowSize!=second.GetRowSize() || m_ColumnSize!=second.GetColumnSize())
- cerr<<"Wrong input.";
- for(int i=0;i<m_RowSize;i++)
- for(int j=0;j<m_ColumnSize;j++)
- Temp(i,j)= -Temp(i,j);
- Temp+=*this;
- return Temp;
- }
- template<class T>
- Matrix<T> Matrix<T>::operator *(const Matrix<T> & second)
- {
- if(m_ColumnSize!=second.GetRowSize() || m_RowSize!=second.GetColumnSize())
- cerr<<"Wrong input.";
- Matrix<T> Temp(m_RowSize,second.GetColumnSize());
- for(int i=0;i<m_RowSize;i++)
- for(int j=0;j<second.GetColumnSize();j++)
- {
- Temp(i,j)=0;
- for(int k=0;k<m_ColumnSize;k++)
- Temp(i,j)+=(m_Data[i][k]*second.GetElement(k,j));
- }
- return Temp;
- }
- template<class T>
- void Matrix<T>::SetSize(int RowSize, int ColumnSize)
- {
- m_RowSize=RowSize;
- m_ColumnSize=ColumnSize;
- m_Data=new T* [m_RowSize];
- for(int i=0;i<m_RowSize;i++)
- m_Data[i]=new T [m_ColumnSize];
- }
- template<class T>
- const int & Matrix<T>::GetRowSize() const
- {
- return m_RowSize;
- }
- template<class T>
- const int & Matrix<T>::GetColumnSize() const
- {
- return m_ColumnSize;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment