Overloading operators
Hi
I need to overload the +,- and * operators to use with matrices.
i have attempted to overload the + operator (at the end of the Matirx.cpp) but im not sure if its right, any help would be great.
(yes i know i spelled matrix wrong haha.)
Matirx.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include "Matrix.h"
Matrix::Matrix()
{
row=0;
col=0;
address=NULL;
}
Matrix::Matrix(int a, int b)
{
if(a<1||b<1)
{
cout << " Wrong num" << endl;
}
address=new double*[a];
for(int i=0;i<a;i++)
address[i]=new double[b];
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
address[i][j]=0;
}
Matrix::Matrix(int a, int b,int c)
{
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
address[i][j]=c;
}
Matrix::~Matrix()
{
for(int i=0;i<row;i++)
delete [] address[i];
delete [] address;
}
Matrix::Matrix(Matrix ©)
{
col=copy.col;
row=copy.row;
address= new double*[copy.row];
for(int i=0;i<row;i++)
address[i]=new double[col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
address[i][j]=copy.address[i][j];
}
Matrix Matrix::operator = (Matrix copy)
{
col=copy.col;
row=copy.row;
address= new double*[copy.row];
for(int i=0;i<row;i++)
address[i]=new double[col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
address[i][j]=copy.address[i][j];
return copy;
}
istream& operator >> (istream &in, Matrix &Mat1)
{
in >> Mat1.row >> Mat1.col;
if(Mat1.address==NULL)
{
Mat1.address=new double*[Mat1.row];
for(int i=0;i<Mat1.row;i++)
Mat1.address[i]=new double[Mat1.col];
}
for(int i=0;i<Mat1.row;i++)
{for(int j=0;j<Mat1.col;j++)
in >> Mat1.address[i][j];}
return in;
}
ostream& operator << (ostream& out, Matrix& Mat1)
{
for(int i=0;i<Mat1.row;i++)
{
for(int j=0;j<Mat1.col;j++)
out << "\t" << Mat1.address[i][j];
out << "\n";
}
out << endl;
return out;
}
Matrix Matrix::operator +(Matrix Mat1)
{
Matrix resultMatrix, Mat2;
if (row != Mat1.row || col != Mat1.col)
cout << "please enter matrices with equal dimensions";
for (int i=0;i<Mat1.row;i++)
for (int j = 0; j < Mat1.col; j++)
resultMatrix.address[i][j] = Mat1.address[i][j] + Mat2.address[i][j];
return resultMatrix;
}
|
Matrix.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
class Matrix
{
private:
int row;
int col;
double** address;
public:
Matrix();
Matrix(int a,int b);
Matrix(int a, int b,int c);
~Matrix();
Matrix(Matrix ©);
Matrix operator +(Matrix);
Matrix operator -(Matrix);
Matrix operator *(Matrix);
Matrix operator *(double);
Matrix operator =(Matrix copy);
friend ostream& operator << (ostream&, Matrix&);
friend istream& operator >> (istream&, Matrix&);
};
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include "Matrix.h"
int main()
{
Matrix M1;
Matrix M2;
cout << " enter " << endl;
cin >> M1;
M2=M1;
cout << "M1 is " << endl;
cout << M1<<endl;
cout << "M2 is " << endl;
cout << M2;
return 0;
}
|
Last edited on
so i made a few changes and im still stumped as to why its not working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
class Matrix
{
private:
int row;
int col;
double** address;
public:
Matrix();
Matrix(int a,int b);
Matrix(int a, int b,int c);
~Matrix();
Matrix(Matrix ©);
Matrix operator + (Matrix Mat1) const;
Matrix operator =(Matrix copy);
friend ostream& operator << (ostream&, Matrix&);
friend istream& operator >> (istream&, Matrix&);
Matrix operator * (Matrix Mat1) const;
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include "Matrix.h"
Matrix::Matrix()
{
row=0;
col=0;
address=NULL;
}
Matrix::Matrix(int a, int b)
{
if(a<1||b<1)
{
cout << " Wrong num" << endl;
}
address=new double*[a];
for(int i=0;i<a;i++)
address[i]=new double[b];
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
address[i][j]=0;
}
Matrix::Matrix(int a, int b,int c)
{
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
address[i][j]=c;
}
Matrix::~Matrix()
{
for(int i=0;i<row;i++)
delete [] address[i];
delete [] address;
}
Matrix::Matrix(Matrix ©)
{
col=copy.col;
row=copy.row;
address= new double*[copy.row];
for(int i=0;i<row;i++)
address[i]=new double[col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
address[i][j]=copy.address[i][j];
}
Matrix Matrix::operator = (Matrix copy)
{
col=copy.col;
row=copy.row;
address= new double*[copy.row];
for(int i=0;i<row;i++)
address[i]=new double[col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
address[i][j]=copy.address[i][j];
return copy;
}
istream& operator >> (istream &in, Matrix &Mat1)
{
in >> Mat1.row >> Mat1.col;
if(Mat1.address==NULL)
{
Mat1.address=new double*[Mat1.row];
for(int i=0;i<Mat1.row;i++)
Mat1.address[i]=new double[Mat1.col];
}
for(int i=0;i<Mat1.row;i++)
{for(int j=0;j<Mat1.col;j++)
in >> Mat1.address[i][j];}
return in;
}
ostream& operator << (ostream& out, Matrix& Mat1)
{
for(int i=0;i<Mat1.row;i++)
{
for(int j=0;j<Mat1.col;j++)
out << "\t" << Mat1.address[i][j];
out << "\n";
}
out << endl;
return out;
}
Matrix Matrix::operator * (Matrix Mat1) const {
Matrix temp, Mat2;
int i,j;
for (int k=0; k<row; k++){
temp.address[i][j] += Mat1.address[i][j] * Mat2.address[i][j];
}
return temp;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include "Matrix.h"
#include <iostream>
using namespace std;
int main()
{
Matrix M1;
Matrix M2;
cout << " enter the dimension" << endl;
cin >> M1;
M2=M1;
Matrix temp;
temp=M1*M2;
cout << "temp is " << endl;
cout << temp <<endl;
return 0;
}
|
any suggestion? i added the overloaded * operator and im getting some errors while compiling.
Topic archived. No new replies allowed.