The products
Published: 2022-08-21 16:25:37

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] (2D-array) 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](2Darray)  	mat[2][2](2Darray)  	mat[2][2](2Darray)  	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)  
4.	Create objects of matrix class: k,l,m. 
5.	Set the elements of matrix k as predefined values  
6.	Input the elements of matrix l  
7.	Perform m = k * l  
8.	Display the values of matrices: k,l and m 
9.	Stop  
  
  
  
  
  
  
  
  
Flowchart:  
  
  
  
 
 
 
 
 Source Code:


#include <iostream>
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;
            
        }


    }

};
 
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;
    }
}

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

    return 0;
}
p2.cpp
Open with ZIP Extractor
1 of 15
 
#include <iostream>
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;
            
        }


    }

};
 
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;
    }
}

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

    return 0;
}
-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.