Matrix operations using operator overloading - GeeksforGeeks

16 Aug, 2021
Pre-requisite: Operator OverloadingGiven two matrix mat1[][] and mat2[][] of NxN dimensions, the task is to perform Matrix Operations using Operator Overloading.Examples:  
Input: arr1[][] = { {1, 2, 3}, {4, 5, 6}, {1, 2, 3}}, arr2[][] = { {1, 2, 3}, {4, 5, 16}, {1, 2, 3}} Output: Addition of two given Matrices is: 2 4 6 8 10 22 2 4 6 Subtraction of two given Matrices is: 0 0 0 0 0 -10 0 0 0 Multiplication of two given Matrices is: 12 18 44 30 45 110 12 18 44 Input: arr1[][] = { {11, 2, 3}, {4, 5, 0}, {1, 12, 3}}, arr2[][] = { {1, 2, 3}, {41, 5, 16}, {1, 22, 3}} Output: Addition of two given Matrices is : 12 4 6 45 10 16 2 34 6 Subtraction of two given Matrices is : 10 0 0 -37 0 -16 0 -10 0 Multiplication of two given Matrices is : 96 98 74 209 33 92 496 128 204  
Approach: To overload +, –, * operators, we will create a class named matrix and then make a public function to overload the operators.  
To overload operator ‘+’ use prototype:  
Return_Type classname :: operator +(Argument list)
{
    // Function Body
}
For Example: 
Let there are two matrix M1[][] and M2[][] of same dimensions. Using Operator Overloading M1[][] and M2[][] can be added as M1 + M2.  
In the above statement M1 is treated hai global and M2[][] is passed as an argument to the function “void Matrix::operator+(Matrix x)“.  
In the above overloaded function, the approach for addition of two matrix is implemented by treating M1[][] as first and M2[][] as second Matrix i.e., Matrix x(as the arguments).  
To overload operator ‘-‘ use prototype: 
Return_Type classname :: operator -(Argument list)
{
    // Function Body
}
For Example: 
Let there are two matrix M1[][] and M2[][] of same dimensions. Using Operator Overloading M1[][] and M2[][] can be added as M1 – M2  
In the above statement M1 is treated hai global and M2[][] is passed as an argument to the function “void Matrix::operator-(Matrix x)“.  
In the above overloaded function, the approach for subtraction of two matrix is implemented by treating M1[][] as first and M2[][] as second Matrix i.e., Matrix x(as the arguments).  
To overload operator ‘*’ use prototype: 
Return_Type classname :: operator *(Argument list)
{
    // Function Body
}
Let there are two matrix M1[][] and M2[][] of same dimensions. Using Operator Overloading M1[][] and M2[][] can be added as M1 * M2.  
In the above statement M1 is treated hai global and M2[][] is passed as an argument to the function “void Matrix::operator*(Matrix x)“.  
In the above overloaded function, the approach for multiplication of two matrix is implemented by treating M1[][] as first and M2[][] as second Matrix i.e., Matrix x(as the arguments).  
Below is the implementation of the above approach: 
C++
// C++ program for the above approach #include "bits/stdc++.h"#define rows 50#define cols 50using namespace std; int N; // Class for Matrix operator overloadingclass Matrix {     // For input Matrix    int arr[rows][cols]; public:    // Function to take input to arr[][]    void input(vector<vector<int> >& A);    void display();     // Functions for operator overloading    void operator+(Matrix x);    void operator-(Matrix x);    void operator*(Matrix x);}; // Functions to get input to Matrix// array arr[][]void Matrix::input(vector<vector<int> >& A){     // Traverse the vector A[][]    for (int i = 0; i < N; i++) {         for (int j = 0; j < N; j++) {             arr[i][j] = A[i][j];        }    }} // Function to display the element// of Matrixvoid Matrix::display(){     for (int i = 0; i < N; i++) {         for (int j = 0; j < N; j++) {             // Print the element            cout << arr[i][j] << ' ';        }        cout << endl;    }} // Function for addition of two Matrix// using operator overloadingvoid Matrix::operator+(Matrix x){    // To store the sum of Matrices    int mat[N][N];     // Traverse the Matrix x    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {             // Add the corresponding            // blocks of Matrices            mat[i][j] = arr[i][j]                        + x.arr[i][j];        }    }     // Display the sum of Matrices    for (int i = 0; i < N; i++) {         for (int j = 0; j < N; j++) {             // Print the element            cout << mat[i][j] << ' ';        }        cout << endl;    }} // Function for subtraction of two Matrix// using operator overloadingvoid Matrix::operator-(Matrix x){    // To store the difference of Matrices    int mat[N][N];     // Traverse the Matrix x    for (int i = 0; i < N; i++) {         for (int j = 0; j < N; j++) {             // Subtract the corresponding            // blocks of Matrices            mat[i][j] = arr[i][j]                        - x.arr[i][j];        }    }     // Display the difference of Matrices    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {             // Print the element            cout << mat[i][j] << ' ';        }        cout << endl;    }} // Function for multiplication of// two Matrix using operator// overloadingvoid Matrix::operator*(Matrix x){    // To store the multiplication    // of Matrices    int mat[N][N];     // Traverse the Matrix x    for (int i = 0; i < N; i++) {         for (int j = 0; j < N; j++) {             // Initialise current block            // with value zero            mat[i][j] = 0;             for (int k = 0; k < N; k++) {                mat[i][j] += arr[i][k]                             * (x.arr[k][j]);            }        }    }     // Display the multiplication    // of Matrices    for (int i = 0; i < N; i++) {         for (int j = 0; j < N; j++) {             // Print the element            cout << mat[i][j] << ' ';        }        cout << endl;    }} // Driver Codeint main(){     // Dimension of Matrix    N = 3;     vector<vector<int> > arr1        = { { 1, 2, 3 },            { 4, 5, 6 },            { 1, 2, 3 } };     vector<vector<int> > arr2        = { { 1, 2, 3 },            { 4, 5, 16 },            { 1, 2, 3 } };     // Declare Matrices    Matrix mat1, mat2;     // Take Input to matrix mat1    mat1.input(arr1);     // Take Input to matrix mat2    mat2.input(arr2);     // For addition of matrix    cout << "Addition of two given"         << " Matrices is : \n";    mat1 + mat2;     // For subtraction of matrix    cout << "Subtraction of two given"         << " Matrices is : \n";    mat1 - mat2;     // For multiplication of matrix    cout << "Multiplication of two"         << " given Matrices is : \n";    mat1* mat2;     return 0;}
Addition of two given Matrices is : 
2 4 6 
8 10 22 
2 4 6 
Subtraction of two given Matrices is : 
0 0 0 
0 0 -10 
0 0 0 
Multiplication of two given Matrices is : 
12 18 44 
30 45 110 
12 18 44 
saurabh1990aror
anikakapoor
ruhelaa48
Operator Overloading
Matrix
Programming Language
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flood fill Algorithm - how to implement fill() in paint?
Multiplication of Matrix using threads
Python program to add two Matrices
Mathematics | L U Decomposition of a System of Linear Equations
Program to find the Sum of each Row and each Column of a Matrix
Modulo Operator (%) in C/C++ with Examples
Differences between Procedural and Object Oriented Programming
Arrow operator -> in C/C++ with Examples
Structures in C++
Top 10 Programming Languages to Learn in 2022