DEV Community

Kitkat
Kitkat

Posted on

The products

Problem:

Overload the binary operator * to multiply the 2x2 matrix. Define a class Matrix with 2-D integer type array with equal size 2. Initialize one object of Matrix k with predefined values. Initialize another object of Matrix l with the user's values. Similarly, create another object n with 0 values to store the result. Now define an operator function to make the expression m = k*l executable.

Problem Analysis:

The problem is to subtract two matrices by using operator overloading. A class named matrix is defined having data members: mat[2]2 and member functions void set(), void display(), one default constructor and one parameterized constructor. During the processing or calculation phase, we need a operator overloading function matrix operator - (matrix mat1) to get resultant of difference of two matrices for this problem.

Input variables Processing

variables/calculations Output variables Necessary heade
files/functions/macr
mat[2]2 mat[2]2 mat[2]2 iostream matrix operator *
( matrix mat1)

Algorithm:

  1. Start
  2. Define a class matrix and its data members and member functions 3. Define an operator overloading function: matrix operator * (matrix mat1)
  3. Create objects of matrix class: k,l,m.
  4. Set the elements of matrix k as predefined values
  5. Input the elements of matrix l
  6. Perform m = k * l
  7. Display the values of matrices: k,l and m
  8. Stop

Flowchart:

Source Code:

include

using namespace std;

class Matrix
{
int mat[2][2];
public:
Matrix(){}
friend Matrix operator*(Matrix mat1, Matrix mat2);

void setMatrix(){
    cout<<"Enter 2x2 matrix: "<<endl;
    for (int i = 0; i < 2; i++)
    {
        for (int k = 0; k < 2; k++)
        {
            cin>> mat[i][k];
        }

    }

}

void printMatrix(){
    cout<<"Here you go: "<<endl;
    for (int i = 0; i < 2; i++)
    {
        for (int k = 0; k < 2; k++)
        {
            cout<< mat[i][k]<<" ";
        }
        cout<<endl;

    }


}
Enter fullscreen mode Exit fullscreen mode

};

Matrix operator*(Matrix mat1, Matrix mat2)
{
{
Matrix mult;
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
for(int k=0; k<2; k++)
{
mult.mat[i][j] += (mat1.mat[i][k] * mat2.mat[k][j]);
}

        }
    }
    return mult;
}
Enter fullscreen mode Exit fullscreen mode

}

int main(){
Matrix a,b,c;
a.setMatrix();
b.setMatrix();
c=a*b;
c.printMatrix();

return 0;
Enter fullscreen mode Exit fullscreen mode

}
p2.cpp
Open with ZIP Extractor
1 of 15

include

using namespace std;

class Matrix
{
int mat[2][2];
public:
Matrix(){}
friend Matrix operator*(Matrix mat1, Matrix mat2);

void setMatrix(){
    cout<<"Enter 2x2 matrix: "<<endl;
    for (int i = 0; i < 2; i++)
    {
        for (int k = 0; k < 2; k++)
        {
            cin>> mat[i][k];
        }

    }

}

void printMatrix(){
    cout<<"Here you go: "<<endl;
    for (int i = 0; i < 2; i++)
    {
        for (int k = 0; k < 2; k++)
        {
            cout<< mat[i][k]<<" ";
        }
        cout<<endl;

    }


}
Enter fullscreen mode Exit fullscreen mode

};

Matrix operator*(Matrix mat1, Matrix mat2)
{
{
Matrix mult;
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
for(int k=0; k<2; k++)
{
mult.mat[i][j] += (mat1.mat[i][k] * mat2.mat[k][j]);
}

        }
    }
    return mult;
}
Enter fullscreen mode Exit fullscreen mode

}

int main(){
Matrix a,b,c;
a.setMatrix();
b.setMatrix();
c=a*b;
c.printMatrix();

return 0;
Enter fullscreen mode Exit fullscreen mode

}
-Discussion and Conclusion:

This program is to multiply two 2x2 matrices by using operator overloading function. Hence, the product of two matrices were performed and displayed. From this lab, I understood the basic structure of C++ programming including the meaning of header files, class, data members and functions, operator overloading function, two dimensional array and steps of problem solving.

Top comments (0)