Saturday 17 September 2011

Matrix using copy constructor and assignment operator


Matrix using copy constructor and assignment operator
Aim:
To write a C++ program for matrix manipulation with dynamic memory allocation using copy constructor and overloading of assignment operator.
Algorithm:
Step 1: Declare the class as Matrix.
Step 2: Declare the data member as r, c and **x.
Step 3: Declare the member functions for constructor, destructor, copy constructor,                  
            getdata, putdata and operator overloading.
Step 4: In the main, create the object for the Matrix class.
Step 5: getdata() function, is used to get the matrix elements.
Step 6: putdata() function is used to display the matrix elements.
Step 7: constructor part is used to allocate the memory space for rows and columns.
Step 8: By overloading the operator “=”, it calls the respective function and performs the
operation.
Step 9: By overloading the operators “+” and “-”, it performs the addition and subtraction
of two matrices and display the result.
Step 10: Destructor is used to destroy the memory space.
Program:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class Matrix
{
int r,c;
int **x;
public:
Matrix(int,int);
Matrix(Matrix &b);
Matrix operator = (Matrix b);
Matrix operator + (Matrix b);
Matrix operator - (Matrix b);
~Matrix();
friend istream & operator >>(istream &, Matrix& );
friend ostream & operator <<(ostream &, Matrix );
};
Matrix :: Matrix(int a=2,int b=2)
{
r=a;c=b;
x=new int*[r];
for(int i=0;i<r;i++)
x[i]=new int[c];

for(i=0;i<r;i++)
for(int j=0;j<c;j++)
{
x[i][j]=0;
}
}
Matrix::Matrix(Matrix &b)
{
r=b.r;c=b.c;
x=new int*[r];
for(int i=0;i<r;i++)
x[i]=new int[c];

for(i=0;i<r;i++)
for(int j=0;j<c;j++)
{
x[i][j]=b.x[i][j];
}
}
Matrix::~Matrix()
{
for(int i=0;i<r;i++)
delete []x[i];
}


Matrix Matrix ::operator = (Matrix b)
{
r=b.r;c=b.c;
x=new int*[r];
for(int i=0;i<r;i++)
x[i]=new int[c];

for(i=0;i<r;i++)
for(int j=0;j<c;j++)
{
x[i][j]=b.x[i][j];
}
return b;
}
Matrix Matrix ::operator + (Matrix b)
{
       Matrix t=b;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
t.x[i][j]=x[i][j]+b.x[i][j];
}
return t;
}
Matrix Matrix ::operator - (Matrix b)
{
Matrix t=b;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
t.x[i][j]=x[i][j]-b.x[i][j];
}
return t;
}
istream & operator >> (istream &in ,Matrix &a)
{
cout<<"\nEnter "<<a.r<<"x"<<a.c<<" Elements"<<endl;
for(int i=0;i<a.r;i++)
for(int j=0;j<a.c;j++)
{
in>>a.x[i][j];
}
return in;
}
ostream & operator << (ostream &ou ,Matrix a)
{
cout<<"\nThe Matrix is:"<<endl;
for(int i=0;i<a.r;i++,cout<<"\n"<<endl)
for(int j=0;j<a.c;j++)
{
ou<<setw(3)<<a.x[i][j]<<"\t";
}
return ou;
}

int main()
{
clrscr();
Matrix a,b,d;
int r,c;
cout<<"\nEnter the Matrix size"<<endl;cin>>r>>c;
a=Matrix(r,c);
cin>>a;
b=a;
cout<<"The a="<<a<<endl;
cout<<"The b="<<b<<endl;
d=a+b;
cout<<"\na+b"<<d<<endl;
d=a-b;
cout<<"\na+b"<<d<<endl;
getch();
return 0;
}

Output:

Enter the matrix of size2x2
2 3
2 3

The matrix A:
   2   3
   2   3

The matrix B:
   2   3
   2   3

The resultant matrix(A+B):
   4   6
   4   6

The resultant matrix(A-B):
  0  0
  0  0

0 comments:

Post a Comment