Matrix Operation using Operator Overloading

Write a program for four atrithmetic operations in matrix using operator overloading.

Questions by shakthisangi

Showing Answers 1 - 9 of 9 Answers

Sanket Akadkar

  • Sep 2nd, 2011
 

#include
#include

class matrix{
private:
int mat[5][5];
int row;
int column;
public:
matrix(int,int);
matrix(matrix&);
void getdata();
matrix operator-(matrix);
matrix operator+(matrix);
matrix operator*(matrix);
void display();
};

matrix::matrix(int r,int c)
{
cout<<"constructor to initiliaze no of rows and columns"< row=r;column=c;
}

matrix::matrix(matrix& m)
{
row=m.row;
column=m.column;

cout<<"Copy constructor"< for(int i=0;i {
for(int j=0;j {
mat[i][j]=m.mat[i][j];
}
}
}

void matrix::getdata()
{
cout<<"Value input:"< for(int i=0;i {
for(int j=0;j {
cout<<"Value("< cin>>mat[i][j];
}
}


}

matrix matrix::operator+(matrix a)
{
cout<<"Addition operator"<
matrix temp(row,column);

for(int i=0;i {
for(int j=0;j {
temp.mat[i][j]=mat[i][j]+a.mat[i][j];
}
}
return temp;
}

matrix matrix::operator-(matrix a)
{
cout<<"Subtraction operator"<
matrix temp(row,column);

for(int i=0;i {
for(int j=0;j {
temp.mat[i][j]=mat[i][j]-a.mat[i][j];
}
}
return temp;
}

matrix matrix::operator*(matrix a)
{
cout<<"Multiplaction operator"<
matrix temp(row,column);

for (int i=0;i {
for (int j=0;j {
temp.mat[i][j]=0;
for(int k=0;k {
temp.mat[i][j]=temp.mat[i][j]+(mat[i][k]*a.mat[k][j]);
}
}
}

return temp;
}

void matrix::display()
{
cout<<"The matrix is "< for(int i=0;i {
for(int j=0;j {
cout< }
cout< }
}

int main()
{
clrscr();
matrix m1(2,2),m2(2,2),m3(2,2);

m1.getdata();
m2.getdata();

m3=m1+m2;
m3.display();

m3=m1-m2;
m3.display();

m3=m1*m2;
m3.display();

getch();
return 0;
}

binoy

  • Aug 23rd, 2012
 

Code
  1. #include<iostream.h>

  2. #include<conio.h>

  3. class matrix

  4. {

  5.   private:long m[5][5];

  6.   int row;int col;

  7.   public:void getdata();

  8.   int operator ==(matrix);

  9.   matrix operator+(matrix);

  10.   matrix operator-(matrix);

  11.   friend ostream & operator << (ostream &,matrix &);

  12. };

  13. /* function to check whether the order of matrix are same or not */

  14. int matrix::operator==(matrix cm)

  15. {

  16.   if(row==cm.row && col==cm.col)

  17.   {

  18.     return 1;

  19.   }

  20.   return 0;

  21. }

  22. /* function to read data for matrix*/

  23. void matrix::getdata()

  24. {

  25.   cout<<"enter the number of rows

  26. ";

  27.   cin>>row;

  28.   cout<<"enter the number of columns

  29. ";

  30.   cin>>col;

  31.   cout<<"enter the elements of the matrix

  32. ";

  33.   for(int i=0;i<row;i++)

  34.   {

  35.     for(int j=0;j<col;j++)

  36.     {

  37.        cin>>m[i][j];

  38.     }

  39.   }

  40. }

  41. /* function to add two matrix */

  42. matrix matrix::operator+(matrix am)

  43. {

  44.   matrix temp;

  45.   for(int i=0;i<row;i++)

  46.   {

  47.     for(int j=0;j<col;j++)

  48.     {

  49.       temp.m[i][j]=m[i][j]+am.m[i][j];

  50.     }

  51.     temp.row=row;

  52.     temp.col=col;

  53.   }

  54.   return temp;

  55. }

  56. /* function to subtract two matrix */

  57. matrix matrix::operator-(matrix sm)

  58. {

  59.   matrix temp;

  60.   for(int i=0;i<row;i++)

  61.   {

  62.     for(int j=0;j<col;j++)

  63.     {

  64.       temp.m[i][j]=m[i][j]-sm.m[i][j];

  65.     }

  66.     temp.row=row;

  67.     temp.col=col;

  68.   }

  69.   return temp;

  70. }

  71. /* function to display the contents of the matrix */

  72. ostream & operator <<(ostream &fout,matrix &d)

  73. {

  74.   for(int i=0;i<d.col;i++)

  75.   {

  76.     for(int j=0;j<d.col;j++)

  77.     {

  78.       fout<<d.m[i][j];

  79.       cout<<" ";

  80.     }

  81.     cout<<endl;

  82.   }

  83. return fout;

  84. }

  85. /* main function */

  86. void main()

  87. {

  88.   matrix m1,m2,m3,m4;

  89.   clrscr();

  90.   m1.getdata();

  91.   m2.getdata();

  92.   if(m1==m2)

  93.   {

  94.     m3=m1+m2;

  95.     m4=m1-m2;

  96.     cout<<"Addition of matrices

  97. ";

  98.     cout<<"the result is

  99. ";

  100.     cout<<m3;

  101.     cout<<"subtraction of matrices

  102. ";

  103.     cout<<"The result is

  104. ";

  105.     cout<<m4;

  106.   }

  107.   else

  108.   {

  109.     cout<<"order of the input matrices is not identical

  110. ";

  111.   }

  112.   getch();

  113. }

  Was this answer useful?  Yes

JoolsL

  • Nov 19th, 2012
 

In both the above answers the op overloads should be using a const ref as input. You should have a (private) ctor that makes the matrix from the data, and should use RVO in the return.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions