Created
July 27, 2016 15:42
-
-
Save ravikiran0606/be3c1dd1cfa155a697d0305602bd91f0 to your computer and use it in GitHub Desktop.
C++ program to implement the use of Dynamic Constructor (Matrix Addition):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
class matrix{ | |
int **a; | |
int m,n; | |
public: | |
matrix(int x,int y); | |
~matrix(); | |
void initialize(); | |
void add(matrix p,matrix q); | |
void display(); | |
}; | |
matrix:: matrix(int x,int y){ | |
m=x; | |
n=y; | |
a=new int*[x]; | |
int i,j; | |
for(i=0;i<x;i++){ | |
a[i]=new int[y]; | |
} | |
for(i=0;i<x;i++){ | |
for(j=0;j<y;j++){ | |
a[i][j]=0; | |
} | |
} | |
} | |
matrix:: ~matrix(){ | |
int i; | |
for(i=0;i<m;i++){ | |
delete a[i]; | |
} | |
} | |
void matrix:: initialize(){ | |
cout<<"\nEnter the elements of the matrix..\n"; | |
int i,j; | |
for(i=0;i<m;i++){ | |
for(j=0;j<n;j++){ | |
cin>>a[i][j]; | |
} | |
} | |
} | |
void matrix:: add(matrix p,matrix q){ | |
int i,j; | |
for(i=0;i<m;i++){ | |
for(j=0;j<n;j++){ | |
a[i][j]=p.a[i][j]+q.a[i][j]; | |
} | |
} | |
} | |
void matrix:: display(){ | |
int i,j; | |
for(i=0;i<m;i++){ | |
for(j=0;j<n;j++){ | |
cout<<a[i][j]<<" "; | |
} | |
cout<<endl; | |
} | |
} | |
int main(){ | |
int p,q,x,y; | |
cout<<"Enter the no of rows and columns of first matrix.."; | |
cin>>x>>y; | |
matrix a(x,y); | |
a.initialize(); | |
cout<<"\nEnter the no of rows and columns of second matrix.."; | |
cin>>p>>q; | |
matrix b(p,q); | |
b.initialize(); | |
matrix c(x,y); | |
c.add(a,b); | |
cout<<"\nThe Addition of the matrices is..\n"; | |
c.display(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment