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:
- Start
- Define a class matrix and its data members and member functions 3. Define an operator overloading function: matrix operator * (matrix mat1)
- Create objects of matrix class: k,l,m.
- Set the elements of matrix k as predefined values
- Input the elements of matrix l
- Perform m = k * l
- Display the values of matrices: k,l and m
- 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;
}
}
};
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
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.
Top comments (0)