What is matrix class? Describe it with example and uses - C++

What is matrix class? Describe it with example and uses.

- A matrix is simply a 2-D array, which is widely used in scientific programming to perform calculations. There are various ways to implement matrix class: using arrays, using pointers, using vectors.
- Let us look at an example of matrix class using pointers :
class matrix
{
   int **p, m, n;
   public:
   matrix(int row = 2, col = 2)
   {
       m = row;
       n = col;
       p = new(int *) [m];
       for (int i = 0; i < m; i++)
          p[i] = new int[n];
   }
   ~matrix()
   {
       for (int i = 0; i < m; i++)
          delete p[i];
       delete p;
   }
   void accept()
   {
       cout<<”Enter matrix elements:”;
       for(int i = 0; i < m; i++)
       {
          for(int j = 0; j < n; j++)
          {
             cin >> p[i][j];
          }
       }
   }
   void display()
   {
       cout <<”The matrix is:”;
       for(int i = 0; i < m; i++)
       {
          cout <<endl;
          for(int j = 0; j < n; j++)
          {
             cout << p[i][j] <<” “;
          }
       }
   }
   matrix operator +(matrix m2)
   {
       matrix T(m, n);
       for(int i = 0; i < m; i++)
       {
          for(int j = 0; j < n; j++)
          {
             T.p[i][j] = p[i][j] + m2.p[i][j];
          }
       }
   return T;
   }
   friend matrix operator * (matrix, matrix);
};

matrix operator * (matrix a , matrix b)
{
   if(a.n == b.m)
   {
       matrix T(a.m, b.n);
       for(int i = 0; i < a.m; i++)
       {
          for(int k = 0; k < b.n; k++)
          {
             T.p[i][k] = 0;
             for(int j = 0; j < a.n; j++)
             {
                  T.p[i][k]+= a.p[i][j] * b.p[j][k];
             }
          }
       }
       return T;
   }
}
- Here, the class supports addition and multiplication functions. We can expand it to have subtraction function.
What are the characteristics of friend functions? - C++
What are the characteristics of friend functions? - A friend function is not in the scope of the class n which it has been declared as friend....
What is a friend function? - C++
What is a friend function? - Private data members cannot be accessed from outside the class. ...
Advantages of using friend classes - C++
Advantages of using friend classes - A friend class and all its member functions have access to all the private members defined within other class. ..
Post your comment