Classes with overloading operators
May 1, 2012 at 7:35am UTC
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
#ifndef matrix_h
#define matrix_h
#include <iostream>
using namespace std;
class Matrix
{
friend ostream& operator <<(ostream output&, const Matrix&);
friend istream& operator >>(istream input&, Matrix&);
public :
Matrix( int c, int r); //default constructor
~Matrix(); //destructor
Matrix( const Matrix & M); //copy constructor
void setMartix( int c, int r); //function for setting the 2D matrix
int getMatrix(); //function to return the matrix
void Setrows( int r);
void Setcols( int c);
int Getcols();
int Getrows();
void Print();
Matrix operator +( const Matrix& M);
Matrix operator -( const Matrix& M);
Matrix operator *( const Matrix& M);
Matrix operator +=( int Matrix& M);
Matrix& operator -();//negation
Matrix& operator =( const Matrix& M);
Matrix& operator ++(); //pre-increment
Matrix& operator ++( int ignoreMe); //post-increment
Matrix& operator --(); //pre-decrement
Matrix& operator --( int ignoreMe); //post decrement
int & operator ()(int row, int cols);
const int & operator ()(int rows, int cols)const ;
bool operator ==( const Matrix& M);
private :
int **array2D;
int rows,cols;
};
#endif
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
#include "Matrix.h"
#include <iostream>
using namespace std;
Matrix::Matrix( int c, int r) //default constructor
{
rows = r;
cols = c;
array2D= new int *[rows];//allocating memory
for ( int i = 0; i < rows; i++)
{
array2D[i] = new int [cols];
for ( int j=0; j < cols; j++)
{
array2D[i][j]=0;
}
}
}
Matrix::Matrix( const Matrix & M) //copy constructor
{
rows = M.rows;
cols = M.cols;
array2D= new int *[rows];
for ( int i = 0; i < rows; i++)
{
array2D[i] = new int [cols];
for ( int j=0; j < cols; j++)
{
array2D[i][j] = M.array2D[i][j];
}
}
}
Matrix::~Matrix() //destructor
{ //deleteing memory to prevent memory leak
for ( int i = 0; i < rows; ++i)
delete [] array2D[i];
delete [] array2D;
}
void Matrix::Setcols( int c) //set column
{
cols = c;
}
void Matrix::Setrows( int r) //set rows
{
rows = r;
}
int Matrix ::Getcols() //get cols
{
return cols;
}
int Matrix ::Getrows() //get rows
{
return rows;
}
bool Matrix:: setMatrix(int value)
{
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
array2D[i][j] = value;
return true ;
}
}
return false ;
}
bool Matrix:: getArr( int & value)
{
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
value = array2D[i][j];
return true ;
}
}
return false ;
}
void Matrix::Print() //print matrix
{
cout<< "The Matrix : " ;
for ( int i =0;i<rows;i++)
for ( int j=0;i<cols;j++)
cout<<array2D[i][j];
}
bool Matrix::operator ==(const Matrix &M)
{
if (this ->rows!=M.rows)
return (false );
if (this ->cols!=M.cols)
return (false );
for (int i=0; i<rows; i++)
{
for (int j=0;j<cols;j++)
{
if (this ->array2D[i][j]!=M.array2D[i][j])
return (false );
}
}
return (true );
}
Matrix& Matrix::operator =(const Matrix& M)
{
if (this ==&M)
return (*this );
delete [] array2D;
this ->rows=M.rows;
this ->cols=M.cols;
this ->array2D = new int *[rows];
for (int i=0; i<rows; i++)
{
this ->array2D[i] = new int [cols];
for (int j=0;j<cols;j++)
{
this ->array2D[i][j]=M.array2D[i][j];
}
}
return (*this );
}
Matrix Matrix::operator +(const Matrix & M)
{
Matrix x(*this );
if (this ->cols!=M.cols)
exit(1);
if (this ->rows!=M.rows)
exit(1);
for (int i=0; i<rows; i++)
{
for (int j=0;j<cols;j++)
{
x.array2D[i][j] = x.array2D[i][j]+M.array2D[i][j];
}
}
return (x);
}
Matrix Matrix::operator -(const Matrix & M)
{
Matrix x(*this );
if (this ->cols!=M.cols)
exit(1);
if (this ->rows!=M.rows)
exit(1);
for (int i=0; i<rows; i++)
{
for (int j=0;j<cols;j++)
{
x.array2D[i][j] = x.array2D[i][j]-M.array2D[i][j];
}
}
return (x);
}
Matrix& Matrix::operator -()//Negate; unary op
{
for (int i=0; i<rows; i++)
{
for (int j=0;j<cols;j++)
{
array2D[i][j] = -array2D[i][j];
}
}
return (*this );
}
Matrix& Matrix::operator ++()//Post increment
{
for (int i=0; i<rows; i++)
{
for (int j=0;j<cols;j++)
{
array2D[i][j]+=1;
}
}
return (*this );
}
Matrix& Matrix::operator ++(int ignoreMe)//Post increment
{
for (int i=0; i<rows; i++)
{
for (int j=0;j<cols;j++)
{
array2D[i][j]++;;
}
}
return (*this );
}
int & Matrix::operator ()(int row, int col)//lvalue; return array2D[row][col];
{
if (row<= 9 || row>=0 && cols<=9 || cols>=0)
return (array2D[rows][cols]);
}
const int & Matrix::operator ()(int row, int col) const //rvalue; return// array2D[row][col];
{
if (row<=9 || row>=0 && cols<=9 || cols>=0)
return (array2D[rows][cols]);
}
Matrix& Matrix:: operator *(const Matrix &M)
{
for (int i=0; i<rows; i++)
{
for (int j=0;j<cols;j++)
{
array2D[i][j] = array2D[i][j]*M.array2D[i][j];
}
}
return (*this );
}
Matrix& Matrix::operator +=(const Matrix &M)
{
for (int i=0;i<rows.i++)
{
for (int j=0;j<cols;j++)
{
array2D[i][j]+=M.array2D;
}
}
return (*this );
}
ostream & operator <<(ostream & output, const Matrix& M)
{
for (int i=0; i<M.rows; i++)
{
for (int j=0;j<M.cols;j++)
{
output<<M.array2D[i][j]<<" " ;
}
}
return output;
}
istream & operator >>(istream & input, const Matrix & M)
{
for (int i=0; i<M.rows; i++)
{
for (int j=0;j<M.cols;j++)
{
input>>M.array2D[i][j];
}
}
return input;
}
can anyone help me figure out whts wrong ??
May 1, 2012 at 7:49am UTC
If this code compiles:
1- Have you ever heard about something called a debugger?
2- Debug your code and tell us where the error happens, and what you expect.
If this code doesn't compile:
1- Tell us where the error happens, point it out in the code.
2- Tell us what the error says exactly.
Put yourself in our shoes and imagine these 1000 lines in front of you... what do you expect us to do?????
May 1, 2012 at 8:05am UTC
The code as given does not compile - there are quie a few syntax errors, spelling errors (where the word Matrix is missspelt), and function redeclaration
errors (function definitions different from function declaration)
Topic archived. No new replies allowed.