Sunday 15 June 2014

Matrix Overload C++ -



Matrix Overload C++ -

in header file suppose add together declaration of functions need overload operators. implement functions added header file.

can 1 help me suppose do? please explain can wrap head around overloading if possible.

i have listed code below. main not suppose edit it.

header file matrix.h

#ifndef matrix_h #define matrix_h class matrix { public: matrix (int sizex, int sizey); ~matrix(); int getsizex() const { homecoming dx; } int getsizey() const { homecoming dy; } long &element(int x, int y); // homecoming reference element void print () const; private: long **p; // pointer pointer long integer int dx, dy; }; #endif /* matrix_h */

matrix.cpp file

#include <iostream> #include <cassert> #include "matrix.h" using namespace std; matrix::matrix (int sizex, int sizey) : dx(sizex), dy(sizey) { assert(sizex > 0 && sizey > 0); p = new long*[dx]; // create array of pointers long integers assert(p != 0); (int = 0; < dx; i++) { // each pointer, create array of long integers p[i] = new long[dy]; assert(p[i] != 0); (int j = 0; j < dy; j++) p[i][j] = 0; } } matrix::~matrix() { (int = 0; < dx; i++) delete [] p[i]; // delete arrays of long integers delete [] p; // delete array of pointers long } long &matrix::element(int x, int y) { assert(x >= 0 && x < dx && y >= 0 && y < dy); homecoming p[x][y]; } void matrix::print () const { cout << endl; (int x = 0; x < dx; x++) { (int y = 0; y < dy; y++) cout << p[x][y] << "\t"; cout << endl; } }

main.cpp

#include <cstdlib> #include <iostream> #include <cassert> #include <ctime> #include "matrix.h" using namespace std; int main(int argc, char** argv) { int size1, size2, size3; const int range = 5; //using generate random number in range[1,6] cout << "please input 3 positive integers: (size)"; cin >> size1 >> size2 >> size3; assert(size1 > 0 && size2 > 0 && size3 > 0); matrix mymatrix1(size1, size2), mymatrix2(size2,size3); matrix yourmatrix1(size1, size2), yourmatrix2(size2, size3); matrix theirmatrix1(size1, size2), theirmatrix2(size1, size3); srand(time(0)); (int = 0; < size1; i++) (int j = 0; j < size2; j++) mymatrix1(i,j) = rand() % range + 1; yourmatrix1 = 2 * mymatrix1; theirmatrix1 = mymatrix1 + yourmatrix1; cout << "mymatrix1: " << endl; cout << mymatrix1 << endl << endl; cout << "yourmatrix1 = 2 * mymatrix " << endl; cout << yourmatrix1 << endl << endl; cout << "mymatrix1 + yourmatrix1: " << endl; cout << theirmatrix1 << endl << endl; (int = 0; < size2; i++) (int j = 0; j < size3; j++) mymatrix2(i,j) = rand() % range + 1; yourmatrix2 = mymatrix2 * 3; theirmatrix2 = mymatrix1 * yourmatrix2; cout << "mymatrix1: " << endl; cout << mymatrix1 << endl << endl; cout << "mymatrix2: " << endl; cout << mymatrix2 << endl << endl; cout << "yourmatrix2 = mymatrix2 * 3 " << endl; cout << yourmatrix2 << endl << endl; cout << "mymatrix1 * yourmatrix2: " << endl; cout << theirmatrix2 << endl << endl; homecoming 0; }

you need main , see operators used matrix objects. have overload operators matrix class given i.e. write fellow member functions class.

refer next link details on operator overloading examples:

http://en.wikibooks.org/wiki/c%2b%2b_programming/operators/operator_overloading

c++ matrix operator-overloading overloading function-overloading

No comments:

Post a Comment