Home »
C++ Programs
C++ program to find the multiplication of two matrices using class
Given two matrices, we have to multiply them using the class and object approach.
Submitted by Shubh Pachori, on August 06, 2022
Problem statement
Given two matrices, we have to multiply them using the class and object approach.
Example:
Input:
Enter Matrix A
Enter x[0][0] : 1
Enter x[0][1] : 2
Enter x[1][0] : 3
Enter x[1][1] : 4
Enter Matrix B
Enter x[0][0] : 4
Enter x[0][1] : 3
Enter x[1][0] : 2
Enter x[1][1] : 1
Matrix A:
1 2
3 4
Matrix B:
4 3
2 1
Output:
Addition of A and B matrix
8 5
20 13
C++ code to find the multiplication of two matrices using class and object approach
#include <iostream>
using namespace std;
// create a class
class Matrix {
// private data members
private:
int x[10][10];
int row, col;
// public functions
public:
// getMatrix() function to insert matrix
void getMatrix(int r, int c) {
// initializing a matrix type variable
Matrix M1;
// copying value of parameters in the data members
row = r;
col = c;
// nested for loop for insertion of matrix
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << "Enter x[" << i << "][" << j << "] : ";
cin >> x[i][j];
}
}
}
// putMatrix() function to print the matrix
void putMatrix() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << x[i][j] << " ";
}
cout << "\n";
}
}
// multiply() function to perform multiplication of two matrix
Matrix multiply(Matrix M2) {
// initializing a Matrix type variable
Matrix M;
// copying the value of parameters in the data members
M.row = row;
M.col = col;
// nested for loop to multiply two matrices
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
M.x[i][j] = 0;
for (int k = 0; k < col; k++) {
M.x[i][j] = M.x[i][j] + ((x[i][k]) * (M2.x[k][j]));
}
}
}
// returning the resulted matrix
return (M);
}
};
int main() {
// creating objects
Matrix M1, M2, M3;
// inserting matrix using getMatrix() function
cout << "Enter Matrix A\n" << endl;
M1.getMatrix(2, 2);
cout << "\nEnter Matrix B\n" << endl;
M2.getMatrix(2, 2);
// printing the matrix using putMatrix() function
cout << "\nMatrix A:" << endl;
M1.putMatrix();
cout << endl;
cout << "Matrix B:" << endl;
M2.putMatrix();
// calling multiply() function to add two matrix
M3 = M1.multiply(M2);
cout << "\nMultiply of A and B matrix\n" << endl;
// printing the resulted matrix
M3.putMatrix();
return 0;
}
Output
Enter Matrix A
Enter x[0][0] : 22
Enter x[0][1] : 33
Enter x[1][0] : 44
Enter x[1][1] : 55
Enter Matrix B
Enter x[0][0] : 66
Enter x[0][1] : 77
Enter x[1][0] : 88
Enter x[1][1] : 99
Matrix A:
22 33
44 55
Matrix B:
66 77
88 99
Multiply of A and B matrix
4356 4961
7744 8833
Explanation
In the above code, we have created a class Matrix, three int type data members x[10][10], row, and col to store the matrix, row, and column, and public member functions getMatrix(), putMatrix() and multiply() to insert, print and multiply matrices.
In the main() function, we are creating an object M1, M2, and M3 of class Matrix, reading matrices given by the user using getMatrix() function, and finally calling the multiply() member function to multiply the given matrices. The multiply() function contains the logic to multiply the given matrices and printing the result.
C++ Class and Object Programs (Set 2) »