123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
/* Creat matrix class and perform addition of 2 matrix using operator+ */ #include <iostream.h> #include <conio.h> #include <math.h> // matrix class class matrix { int row, col; float **mat; public: matrix(int, int); // constructor matrix(matrix &); // copy constructor ~matrix(); // void display(); void accept(); friend matrix operator+(matrix, matrix); }; matrix::matrix(int r, int c) { row=r; col=c; mat = new float*[row]; for(int i=0; i<row; i++) { mat[i]= new float[col]; for(int j=0;j<col;j++) mat[i][j]=0; } } matrix::matrix(matrix &a) { row=a.row; col=a.col; for(int i=0; i<row; i++) for(int j=0;j<col;j++) mat[i][j]=a.mat[i][j]; } matrix::~matrix() { for (int i=0;i<row;i++) delete mat[i]; delete mat; } void matrix::display() { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) cout<<mat[i][j]<<" "; cout<<" \n"; } } void matrix::accept() { int i,j; cout<<"Number of row: "<<row<<endl; cout<<"Number of colum : "<<col<<endl; for(i=0;i<row;i++) for(j=0;j<col;j++) { cout<<"Accept the element ("<<i<<","<<j<<") : "; cin>>mat[i][j]; } } matrix operator+(matrix mat1, matrix mat2) { matrix c(2,2); for(int i=0;i<c.row;i++) for(int j=0;j<c.col;j++) c.mat[i][j]=mat1.mat[i][j]+mat2.mat[i][j]; return c; } int main() { clrscr(); matrix a(2,2),b(2,2),c(2,2); a.accept(); a.display(); b.accept(); b.display(); c=a+b; c.display(); getch(); return 0; }