Matrix Class C++ Example

Define matrix class with dynamic memory allocation for 2-D matrix. Include constructors, destructor, overload + (m1+m2, m1+3 & 4+m2), -, << and >> operators. Also overload new and delete operators by using malloc & free functions internally

#include<iostream>
using namespace std;
#include<cstdlib>
#include<new>
class matrix
{

int **array;
int row;
int column;

public:

//default constructor
matrix()
{

array=NULL;
row=0;
column=0;
}

//single parameter constructor(square matrix)
matrix(int length)
{

int i,j;
array=(int **)new int[length];
row=length;
column=length;
for(i=0;i<length;i++)
array[i]=new int[length];

for(i=0;i<length;i++)
for(j=0;j<length;j++)
array[i][j]=0;
}


//double parameterized constructor(rectangular matrix)
matrix(int r,int c)
{
int i,j;
row=r;
column=c;
array=(int **)new int[r];

for(i=0;i<r;i++)
array[i]=new int[c];

for(i=0;i<r;i++)
for(j=0;j<c;j++)
array[i][j]=0;
}

//copy constructor

matrix(matrix &c)
{
int i,j;
array=(int **)new int[c.row];

for(i=0;i<c.row;i++)
array[i]=new int[c.column];
row=c.row;
column=c.column;
for(i=0;i<c.row;i++)
for(j=0;j<c.column;j++)
array[i][j]=0;
}

//destructor
~matrix()
{
delete []array;
}

//OVerloading matrix "+" operation

matrix operator+(const matrix &c)
{
int i,j;
matrix temp;
if(row!=c.row||column!=c.column)
{
cout<<"\n addition is not possible";
exit(1);
}
temp.row=row;
temp.column=column;
temp.array=(int **)new int[row];
for(i=0;i<row;i++)
temp.array[i]=new int[column];
for(i=0;i<row;i++)
for(j=0;j<column;j++)
temp.array[i][j]=array[i][j]+c.array[i][j];

return temp;
}

matrix operator=(const matrix &mat)
{
int i,j;

if(row!=mat.row || column!=mat.column)
{
cout<<"\n\nAssignment of different matrices is not possible..\n";
exit(1);
}

for(i=0;i<row;i++)
for(j=0;j<column;j++)
array[i][j]=mat.array[i][j];

return (*this);
}

matrix operator-(const matrix &c)
{
int i,j;
matrix temp;
if(row!=c.row||column!=c.column)
{
cout<<"\n addition is not possible";
exit(1);
}
temp.array=(int **)new int[row];
for(i=0;i<row;i++)
temp.array[i]=new int[column];
for(i=0;i<row;i++)
for(j=0;j<column;j++)
temp.array[i][j]=array[i][j]-c.array[i][j];
temp.row=row;
temp.column=column;
return temp;
}
matrix operator+(int value)
{
int i,j;
matrix temp;
temp.array=(int **)new int[row];
for(i=0;i<row;i++)
temp.array[i]=new int[column];
for(i=0;i<row;i++)
for(j=0;j<column;j++)
temp.array[i][j]=array[i][j]+value;
temp.row=row;
temp.column=column;
return temp;
}

matrix operator-(int value)
{
int i,j;
matrix temp;
temp.array=(int **)new int[row];
for(i=0;i<row;i++)
temp.array[i]=new int[column];
for(i=0;i<row;i++)
for(j=0;j<column;j++)
temp.array[i][j]=array[i][j]-value;
temp.row=row;
temp.column=column;
return temp;
}


void * operator new[](size_t size)
{
void *pointer=malloc(size);
return pointer;
}

void  operator delete[](void *pointer)
{
free(pointer);
}
friend matrix operator+(int value,matrix c);

friend matrix operator-(int value,matrix c);

friend istream& operator>>(istream &cin,matrix &c);

friend ostream& operator<<(ostream &cout,matrix &c);


};
istream& operator>>(istream &cin,matrix &c)
{

int i,j;
cout<<"\n enter array values"<<endl;
for(i=0;i<c.row;i++)
{
for(j=0;j<c.column;j++)
{

cout<<"\n enter value of array["<<i<<"]["<<j<<"]";
cin>>c.array[i][j];
}
}
return cin;
}

ostream& operator<<(ostream &cout,matrix &c)
{

int i,j;
cout<<"\n enter array values"<<endl;
for(i=0;i<c.row;i++)
{
for(j=0;j<c.column;j++)
{

cout<<"\n the value of array["<<i<<"]["<<j<<"]";
cout<<c.array[i][j];
}
}
return cout;
}
matrix operator+(int value,matrix c)
{

matrix temp;
int i,j;
temp.array=(int **)new int[c.row];
for(i=0;i<c.row;i++)
temp.array[i]=new int[c.column];
temp.row=c.row;
temp.column=c.column;

for(i=0;i<c.row;i++)
for(j=0;j<c.column;j++)
temp.array[i][j]=value+c.array[i][j];
return temp;
}
matrix operator-(int value,matrix c)
{

matrix temp;
int i,j;
temp.array=(int **)new int[c.row];
for(i=0;i<c.row;i++)
temp.array[i]=new int[c.column];
temp.row=c.row;
temp.column=c.column;

for(i=0;i<c.row;i++)
for(j=0;j<c.column;j++)
temp.array[i][j]=value-c.array[i][j];
return temp;
}
int main()
{
matrix m1(2),m2(2),m3(2),m4(2);
cout<<"\n enter matrix m1 values";
cin>>m1;
cout<<"\n the values of matrix1 entered are";
cout<<m1;
cout<<"\n enter matrix m2 values";
cin>>m2;
cout<<m2;
//cin>>m3;
cout<<"\n the values of matrix 2entered are";
cout<<m2;
m3=m1+m2;
cout<<"\n the values of matrix3 after addition are";
cout<<m3;
m4=m1-m2;
cout<<"\n the values of matrix 4 after subtraction are";
cout<<m4;

}



Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto