Matrix addition multiplication using operator overloading in C++
Let’s begin to code the Matrix addition multiplication using operator overloading in C++. First of all, we will see how to add a matrix using operator overloading in C++.
Matrix addition using operator overloading in C++
T4Tutorials is the class name. formal_parameters indicates the formal parameter that takes the value from the actual parameters. operator+ indicates that we want to overload the + operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//+ operator is overloaded T4Tutorials T4Tutorials::operator+(T4Tutorials formal_parameters) { T4Tutorials t; t.u=u; t.v=v; cout<<t.u; cout<<t.v; for(int i=0;i<t.u;i++) for(int j=0;j<t.v;j++) t.MyArray[i][i]=MyArray[i][i]+formal_parameters.MyArray[i][i]; return t; } |
Matrix multiplication using operator overloading in C++
Here, the operator * indicates that we want to overload the + operator.
Now, we will see how to multiply matrix using operator overloading in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//* operator is overloaded T4Tutorials T4Tutorials::operator*(T4Tutorials formal_parameters) { T4Tutorials t; t.u=u; t.v=formal_parameters.v; for(int i=0;i<t.u;i++) for(int j=0;j<t.v;j++) { t.MyArray[i][i]=0; for(int k=0;k<v;k++) t.MyArray[i][j]+=MyArray[i][k]*formal_parameters.MyArray[k][j]; } return t; } |
Matrix addition and multiplication using operator overloading in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include<iostream> #include<conio.h> using namespace std; class T4Tutorials { private: int MyArray[10][10]; int u,v; public: void show(); T4Tutorials operator +(T4Tutorials); T4Tutorials operator *(T4Tutorials); void read(); }; //+ operator is overloaded T4Tutorials T4Tutorials::operator+(T4Tutorials formal_parameters) { T4Tutorials t; t.u=u; t.v=v; cout<<t.u; cout<<t.v; for(int i=0;i<t.u;i++) for(int j=0;j<t.v;j++) t.MyArray[i][i]=MyArray[i][i]+formal_parameters.MyArray[i][i]; return t; } //* operator is overloaded T4Tutorials T4Tutorials::operator*(T4Tutorials formal_parameters) { T4Tutorials t; t.u=u; t.v=formal_parameters.v; for(int i=0;i<t.u;i++) for(int j=0;j<t.v;j++) { t.MyArray[i][i]=0; for(int k=0;k<v;k++) t.MyArray[i][j]+=MyArray[i][k]*formal_parameters.MyArray[k][j]; } return t; } void T4Tutorials::read() { cout<<"Please Enter Size of Matrix like 2 x 2:\n"; cin>>u>>v; cout<<"Please Enter the Elements in the Matrix :\n"; for(int i=0;i<u;i++) for(int j=0;j<v;j++) cin>>MyArray[i][j]; } void T4Tutorials::show() { for(int i=0;i<u;i++) { for(int j=0;j<v;j++) { cout<<MyArray[i][j]<<"\t"; } cout<<"\n"; } } int main() { T4Tutorials object1 ,object2,object3; cout<<"Please Enter First Matrix\n"; object1.read(); cout<<"Please Enter Second Matrix\n"; object2.read(); object3=object1 +object2; cout<<"Result After Addition of two Matrix\n"; object3.show(); object3=object1 *object2; cout<<"Result After Multiplication of two Matrix\n"; object3.show(); getch(); } |
Output