Download this source file (Matrix1.h)
// matrix1.h - a simple 3 x 3 matrix class. // Each element is of type int. // Written by Wayne Pollock, Tampa FL, 2000. #ifndef MATRIX_H #define MATRIX_H class matrix { enum { dim = 3 }; // The dimensions of a matrix public: matrix (); // Default Constructor matrix ( int m[dim][dim] ); // Conversion Constructor matrix ( const matrix& mat ); // Copy Constructor matrix& operator= ( const matrix& mat ); // Overloaded Assignment Operator void print () const; // Prints the matrix to cout matrix operator+ ( const matrix& mat ) const; // Overloaded + Operator matrix operator* ( const matrix& mat ) const; // Overloaded * Operator private: int rep[dim][dim]; // The "rep"resentation. }; #endif
Download this source file (MatMain1.cpp)
// matmain1.cpp - a file that uses the user-defined type matrix. // Written by Wayne Pollock, Tampa FL, 2000. #include #include "matrix1.h" using namespace std; int main () { int ary1[3][3] = { 1, 1, 1, 2, 2, 2, 3, 3, 3 }; int ary2[3][3] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; matrix m1, m2(ary1); matrix ident = ary2; cout << endl << "m1 = (Default):"; m1.print(); cout << endl << "m2 = (array of ints)"; m2.print(); matrix m3 = m2 + m2; // matrix m3 = m1.operator+( m2 ); cout << endl << "m3 = m2 + m2:"; m3.print(); matrix m4 = m2 * m2; cout << endl << "m4 = m2 * m2:"; m4.print(); m1 = ident * m3 + m2; cout << endl << "m1 = ident * m3 + m2:"; m1.print(); return 0; } #ifdef COMMENTED_OUT // Output of Above Program: m1 = (Default): { 0, 0, 0, 0, 0, 0, 0, 0, 0, } m2 = (array of ints) { 1, 1, 1, 2, 2, 2, 3, 3, 3, } m3 = m2 + m2: { 2, 2, 2, 4, 4, 4, 6, 6, 6, } m4 = m2 * m2: { 6, 6, 6, 12, 12, 12, 18, 18, 18, } m1 = ident * m3 + m2: { 3, 3, 3, 6, 6, 6, 9, 9, 9, } #endif
Download this source file (Matrix1.cpp)
// matrix1.cpp - The implementation of the class matrix. // Written by Wayne Pollock, Tampa FL, 2000. #include #include #include "matrix1.h" using namespace std; matrix::matrix () { for ( int i = 0; i < dim; ++i ) for ( int j = 0; j < dim; ++j ) rep[i][j] = 0; } matrix::matrix ( int m[dim][dim] ) { for ( int i = 0; i < dim; ++i ) for ( int j = 0; j < dim; ++j ) rep[i][j] = m[i][j]; } matrix::matrix ( const matrix& mat ) { for ( int i = 0; i < dim; ++i ) for ( int j = 0; j < dim; ++j ) rep[i][j] = mat.rep[i][j]; } matrix& matrix::operator= ( const matrix& mat ) { for ( int i = 0; i < dim; ++i ) for ( int j = 0; j < dim; ++j ) rep[i][j] = mat.rep[i][j]; return *this; } void matrix::print () const { cout << endl << " {" << endl; for ( int i = 0; i < dim; ++i ) { cout << " "; for ( int j = 0; j < dim; ++j ) cout << setw( 2 ) << rep[i][j] << ", "; cout << endl; } cout << " }" << endl; } matrix matrix::operator+ ( const matrix& mat ) const { matrix temp = mat; for ( int i=0; i
Download this source file (makefile)
# Makefile to build Matrix example C++ programs. # Written by Wayne Pollock, Tampa Florida, 2/2000. all: mat1.exe mat2.exe mat1.exe: matmain1.obj matrix1.obj bcc32 -emat1.exe matmain1.obj matrix1.obj mat2.exe: matmain2.obj matrix2.obj bcc32 -emat2.exe matmain2.obj matrix2.obj clean: del mat*.obj del mat*.tds cleanall: clean del mat*.exe