Anonymous School
(Minds of Hacker)
--------------------------------
Create a class MAT which is used to represent Matrix. This class contains two dimensional array, rows, columns of that matrix as data member. Perform addition of matrix using MAT objects with operator overloading. (you can use binary addition operator)
#include<iostream>using namespace std;class MAT{int a[3][3];public:void accept();void display();void operator +(MAT x);};void MAT::accept(){cout<<"\n Enter MATRIX Element (3 X 3) : \n";for(int i=0; i<3; i++){for(int j=0; j<3; j++){cout<<" ";cin>>a[i][j];}}}void MAT::display(){for(int i=0; i<3; i++){cout<<" ";for(int j=0; j<3; j++){cout<<a[i][j]<<"\t";}cout<<"\n";}}void MAT::operator +(MAT x){int mat[3][3];for(int i=0; i<3; i++){for(int j=0; j<3; j++){mat[i][j]=a[i][j]+x.a[i][j];}}cout<<"\n Addition of MATRIX : \n\n";for(int i=0; i<3; i++){cout<<" ";for(int j=0; j<3; j++){cout<<mat[i][j]<<"\t";}cout<<"\n";}}int main(){MAT m,n;m.accept(); // Accepting Rowsn.accept(); // Accepting Columnscout<<"\n First MATRIX : \n\n";m.display(); // Displaying First MATcout<<"\n Second MATRIX : \n\n";n.display(); // Displaying Second MATm+n; // Addition of Two Matrices. Overloaded '+' Operatorreturn 0;}
Output:
Enter MATRIX Element (3 X 3) :
1 2 3
4 5 6
7 8 9
Enter MATRIX Element (3 X 3) :
10 11 12
13 14 15
16 17 18
First MATRIX :
1 2 3
4 5 6
7 8 9
Second MATRIX :
10 11 12
13 14 15
16 17 18
Addition of MATRIX :
11 13 15
17 19 21
23 25 27
*******************Don't Make Learning Hard*****************
Follow us on :-
1) YouTube Channel :- Click Here
2) Blog :- Click Here