Multiplication of two matrix using operator overloading In C++

C++

Multiplication of two matrix using operator overloading In C++CPP OPP’s In Hindi – Is C++ OOPs Me Ek C++ Matrix Program Ko Create Karne Wale Hai.

Also Read – C++ Constructors and Destructors

Multiplication of two matrix using operator overloading In C++

C++ Operator Overloading in Hindi Ek Aesi Process Hai, Jisme Ek Operator Symbol Ka Istemal Alag Alag Kaam Karne Ke Liye Kiya Jata Hai, Isme Ham C++ Me Pahle Se Available Operators Ko New Meaning Dekar Istemal Karte Hai|

Example:

#include<iostream>
using namespace std;
class Matrix
{
	int a[10][10],m,n;
	public:
		void inputmatrices();
		void outputmatrices();
		Matrix operator*(Matrix x);
};
void Matrix::inputmatrices()
{
	int i,j;
	cout<<"Enter order of matrix:";
	cin>>m>>n;
	cout<<"Enter matrix elements:";
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
}
void Matrix::outputmatrices()
{ 
	int i,j;
	for(i=0;i<m;i++)
	{ 
		for(j=0;j<n;j++)
		{
			cout<<a[i][j]<<"\t";
			cout<<endl; 
		}
	}
}

Matrix Matrix::operator*(Matrix x)
{ 
	Matrix c;
	int i,j,k;
	if(n==x.m)
	{ 
		c.m=m;
		c.n=x.n;
		for(i=0;i<m;i++)
		{
			for(j=0;j<x.n;j++)
			{ 
				c.a[i][j]=0;
			}
		}
		for(k=0;k<m;k++)
		{
			c.a[i][j]+=a[i][k]*x.a[k][j];
		}
		return c; 
	}
	else
		cout<<"Multiplication is not possible.";
}
int main()
{ 
Matrix A,B,C;
cout<<"Enter matrix A order and elements:"<<endl;
A.inputmatrices();
cout<<"Enter matrix B order and elements:"<<endl;
B.inputmatrices();
C=A*B;
cout<<"Resulting matrix is:"<<endl;
C.outputmatrices();
return 0;
}

Output:

Enter matrix A order and elements:
Enter order of matrix:2 2
Enter matrix elements:1 2 3 4
Enter matrix B order and elements:
Enter order of matrix:2 2
Enter matrix elements:1 2 3 4
Resulting matrix is:
7 	10
15 	22

Also Read – Shopping List Program

Aap Upper Output Ko Dekh Kar Program Ko Understand Kar Sakte Hai, isme Hame Class Matrix Create Kiya And Fir Unke Value Ko Input Karaya Hai, Uske Bad Hame Unhe Print Kar Diya Hai |

Leave a Reply

Your email address will not be published. Required fields are marked *