class matrix { public: // these are overloaded operators - 3 styles void operator = (const matrix); float * operator() (const int, const int) ; // this is a constructor function matrix (int m, int n) // default initialization { rows = m; //set the members cols = n; pt = new float[rows*cols]; } // these are the variable members - they are private private: int rows, cols; float *pt; }; // note the ; // overloaded definitions void matrix :: operator =(const matrix arg) { int i; if( (rows != arg.rows) || (cols != arg.cols) ) { cout << "\n You cannot set different sized"; cout << " matrices equal to each other. \n"; //exit(EXIT_FAILURE); } for (i=0; i < rows*cols; i++) *(pt+i) = *(arg.pt+i); } float * matrix :: operator() ( const int row, const int col) { return pt + (row * cols + col); } main (int argc, int *argv[]) { int i, j; // We can run with variable matrix size without re-compiling if (argc != 3) // the program name and 2 variables = 3 { cout << "You must include 2 values with the name of the program\n" << " matrix #rows #cols. \n " << " Try again.\n"; // exit (EXIT_FAILURE); } int rows = (argv[1]); // I trust that the user int cols = (argv[2]); // will input good values matrix A(rows, cols); matrix B(rows,cols); matrix C(rows-2,cols-2); for (i=0; i < rows; i++) for (j=0; j < cols; j++) *A(i,j) = i + j; B = A; cout << "\n The A matrix \n"; for (i=0; i < rows; i++) { for (j=0; j < cols; j++) cout << *A(i,j) << " "; cout << "\n"; } cout << "\n The B matrix \n"; for (i=0; i < rows; i++) { for (j=0; j < cols; j++) cout << *B(i,j) << " "; cout << "\n"; } C = A; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)