Guest User

Matrix class

a guest
Nov 23rd, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3.  
  4. #include<iostream>
  5. using namespace std;
  6.  
  7.  
  8. template<class T>
  9. class Matrix
  10. {
  11. private:
  12.     int m_RowSize;
  13.     int m_ColumnSize;
  14.     T**m_Data;
  15.  
  16. public:
  17.     Matrix(int RowSize,int ColumnSize);
  18.     Matrix()
  19.     {
  20.         m_RowSize=0;
  21.         m_ColumnSize=0;
  22.     }
  23.     Matrix(const Matrix<T> &);
  24.     ~Matrix(void)
  25.     {
  26.       for(int i=0;i<m_RowSize;i++)
  27.         delete[] m_Data[i];
  28.       delete [] m_Data;
  29.     }
  30.  
  31.     void SetElement(int Row,int Column,const T& Value);
  32.     void SetInput();
  33.     T GetElement(int Row ,int Column) const ;
  34.     void GetOutput();
  35.     bool operator!() const;
  36.     T &operator() (int Row,int Column);
  37.     Matrix<T> operator+(const Matrix<T> &);
  38.     const Matrix<T> & operator+=(const Matrix<T> &);
  39.     Matrix<T>  operator - (const Matrix<T> &);
  40.     Matrix<T>  operator* (const Matrix<T> &);
  41.     void SetSize(int RowSize,int ColumnSize);
  42.     const int& GetColumnSize() const;
  43.     const int & GetRowSize() const;
  44.  
  45. };
  46.  
  47. template<class T>
  48. Matrix<T>::Matrix(int RowSize,int ColumnSize)
  49. :m_RowSize(RowSize),m_ColumnSize(ColumnSize)
  50. {
  51.     m_Data=new T *[m_RowSize];
  52.     for(int i=0;i<m_RowSize;i++)
  53.     {
  54.         m_Data[i]=new T [m_ColumnSize];
  55.     }
  56.  
  57. }
  58.  
  59. template<class T>
  60. Matrix<T>::Matrix(const Matrix<T> & second)
  61. {
  62.     m_RowSize=second.GetRowSize();
  63.     m_ColumnSize=second.GetColumnSize();
  64.     m_Data=new T *[m_RowSize];
  65.     for(int i=0;i<m_RowSize;i++)
  66.     {
  67.         m_Data[i]=new T [m_ColumnSize];
  68.     }
  69.     for(int i=0;i<m_RowSize;i++)
  70.         for(int j=0;j<m_ColumnSize;j++)
  71.             m_Data[i][j]=second.GetElement(i,j);
  72. }
  73.  
  74. template<class T>
  75. void Matrix<T>::SetElement(int Row,int Column,const T& Value)
  76. {
  77.     if(m_RowSize>Row && m_ColumnSize>Column)
  78.     m_Data[Row][Column]=Value;
  79.  
  80. }
  81.  
  82. template<class T>
  83. T Matrix<T>::GetElement(int Row, int Column) const
  84. {if(m_RowSize>Row && m_ColumnSize>Column)
  85.     return m_Data[Row][Column];
  86. else cerr<<"Wrong input.";
  87. }
  88.  
  89. template<class T>
  90. void Matrix<T>::SetInput()
  91. {
  92.     for(int i=0;i != m_RowSize;i++)
  93.     { for (int j=0;j!=m_ColumnSize;j++){
  94.         cout<<"Enter entry:";
  95.         T a;
  96.         cin>>a;
  97.         SetElement(i,j,a);}
  98.     cout<<endl;}
  99. }
  100.  
  101.  
  102. template<class T>
  103. void Matrix<T>::GetOutput()
  104. {
  105.     for(int i=0;i!= m_RowSize;i++)
  106.     { for (int j=0;j!=m_ColumnSize;j++){
  107.         cout<<endl;
  108.         cout<<GetElememt(i,j);}
  109.     cout<<endl;}
  110. }
  111.  
  112. template<class T>
  113. bool Matrix<T>::operator !() const
  114. {
  115.     return (m_RowSize==0);
  116. }
  117.  
  118. template<class T>
  119.  T & Matrix<T>::operator ()(int Row,int Column)
  120. {
  121.     if(m_RowSize>Row && m_ColumnSize>Column)
  122.     return m_Data[Row][Column];
  123.     else cerr<<"Wrong input.";
  124. }
  125.  
  126. template<class T>
  127. const Matrix<T> & Matrix<T>::operator+=(const Matrix<T> & second)
  128. {
  129.     if(m_RowSize!=second.GetRowSize() || m_ColumnSize!=second.GetColumnSize())
  130.  
  131.         cerr<<"Wrong input.";
  132.         for(int i=0;i<m_RowSize;i++)
  133.             for(int j=0;j<m_ColumnSize;j++)
  134.             {
  135.                 m_Data[i][j]+=second.GetElement(i,j);
  136.             }
  137.             return *this;
  138. }
  139.  
  140. template<class T>
  141.  Matrix<T>  Matrix<T>::operator +(const Matrix<T> & second)
  142. {
  143.     Matrix<T> Temp(second);
  144.     if(m_RowSize!=second.GetRowSize() || m_ColumnSize!=second.GetColumnSize())
  145.  
  146.         cerr<<"Wrong input.";
  147.               Temp+=*this;
  148.         return Temp;
  149. }
  150.  
  151. template<class T>
  152.  Matrix<T> Matrix<T>::operator -(const Matrix<T> & second)
  153. {
  154.     Matrix<T> Temp(second);
  155.     if(m_RowSize!=second.GetRowSize() || m_ColumnSize!=second.GetColumnSize())
  156.     cerr<<"Wrong input.";
  157.     for(int i=0;i<m_RowSize;i++)
  158.         for(int j=0;j<m_ColumnSize;j++)
  159.             Temp(i,j)= -Temp(i,j);
  160.     Temp+=*this;
  161.     return Temp;
  162. }
  163.  
  164. template<class T>
  165.  Matrix<T>  Matrix<T>::operator *(const Matrix<T> & second)
  166. {
  167.     if(m_ColumnSize!=second.GetRowSize() || m_RowSize!=second.GetColumnSize())
  168.         cerr<<"Wrong input.";
  169.     Matrix<T> Temp(m_RowSize,second.GetColumnSize());
  170.     for(int i=0;i<m_RowSize;i++)
  171.         for(int j=0;j<second.GetColumnSize();j++)
  172.         {
  173.             Temp(i,j)=0;
  174.             for(int k=0;k<m_ColumnSize;k++)
  175.                 Temp(i,j)+=(m_Data[i][k]*second.GetElement(k,j));
  176.         }
  177.         return Temp;
  178. }
  179.  
  180. template<class T>
  181. void Matrix<T>::SetSize(int RowSize, int ColumnSize)
  182. {
  183.     m_RowSize=RowSize;
  184.     m_ColumnSize=ColumnSize;
  185.     m_Data=new T* [m_RowSize];
  186.     for(int i=0;i<m_RowSize;i++)
  187.         m_Data[i]=new T [m_ColumnSize];
  188.  
  189. }
  190.  
  191. template<class T>
  192. const int & Matrix<T>::GetRowSize() const
  193. {
  194.     return m_RowSize;
  195. }
  196.  
  197. template<class T>
  198. const int & Matrix<T>::GetColumnSize() const
  199. {
  200.     return m_ColumnSize;
  201. }
  202.  
  203. #endif
  204.  
  205.  
  206.  
Advertisement
Add Comment
Please, Sign In to add comment