CS201-44 - Introduction to Programming Lecture 44 Class...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 44 Class Matrix class Matrix { private : int numRows , numCols ; double ** elements ; } ; class Matrix { private : int numRows , numCols ; double ** elements ; public : Matrix ( int = 0 , int = 0 ) ; // Default constructor Matrix ( const Matrix & ) ; // Copy constructor ~ Matrix ( ) ; // Destructor Class Matrix // Utility functions of Matrix class int getRows ( void ) const ; int getCols ( void ) const ; Class Matrix // Input output functions for Matrix class const Matrix & input ( istream & is = cin ) ; const Matrix & input ( ifstream & is ) ; void output ( ostream & os = cout ) const ; void output ( ofstream & os ) const ; Class Matrix // Plus Operator Matrix operator + ( Matrix & m ) const ; Matrix operator + ( double d ) const ; A + d ; ‘A’ is an object of a class Matrix d is a variable of type double d + A ; d is a variable of type double ‘a’ is an object of a class Matrix Class Matrix // Plus Operator Matrix operator + ( Matrix & m ) const ; Matrix operator + ( double d ) const ; friend Matrix operator + ( double d , Matrix & m ) ; const Matrix & operator += ( Matrix & m ) ; i += 3 ; i = i + 3 ; A += B ; // A and B are Matrices A – B Where A and B are both matrices ‘A’ is an object of a class Matrix d is a variable of type double A – d ; d – A ; d is a variable of type double ‘a’ is an object of a class Matrix Class Matrix // Minus Operator Matrix operator ­ ( Matrix & m ) const ; Matrix operator ­ ( double d ) const ; friend Matrix operator ­ ( double d , Matrix & m ) ; Where A and B are both matrices A * B ; A * d ; ‘A’ is an object of a class Matrix d is a variable of type double d * A ; d is a variable of type double ‘a’ is an object of a class Matrix Class Matrix // Multiplication Operator Matrix operator * ( const Matrix & m ) ; Matrix operator * ( double d ) const ; friend Matrix operator * ( const double d , const Matrix & m ) ; A / d ; ‘A’ is an object of a class Matrix d is a variable of type double Class Matrix // Division Operator Matrix operator / ( const double d ) ; Example // Stream Insertion and Extraction Operator cin >> m ; // Where m is a matrix Class Matrix // Stream Insertion and Extraction Operator friend istream & operator >> ( istream & , Matrix & ) ; friend ifstream & operator >> ( ifstream & , Matrix & ) ; friend istream & operator << ( istream & , Matrix & ) ; friend ifstream & operator << ( ifstream & , Matrix & ) ; Class Matrix const Matrix & operator = ( const Matrix & m ) ; const Matrix & transpose ( void ) ; Matrix :: Matrix ( int row , int col ) // Default Constructor { numRows = row ; numCols = col ; elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ ) { elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = 0.0 ; } } Class Matrix Matrix A ( B ) ; Matrix A = B ; Matrix :: Matrix ( const Matrix & m ) { numRows = m.numRows ; numCols = m.numCols ; elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ ) { elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = m.elements [ i ] [ j ] ; } } Class Matrix Class Matrix Matrix :: ~ Matrix ( void ) { delete [ ] elements ; } Class Matrix int Matrix :: getRows ( ) const { } return numRows ; int Matrix :: getCols ( ) const { return numCols ; } Class Matrix void Matrix :: output ( ostream & os ) const { // Print first row with special characters os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ; os << ( char ) 218 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << " “ ; os << ( char ) 191 << "\n" ; Class Matrix // Print remaining rows with vertical bars only for ( int i = 0 ; i < numRows ; i ++ ) { os << ( char ) 179 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << ( char ) 179 << "\n" ; } Class Matrix // Print last row with special characters os << ( char ) 192 ; for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 10 ) << " " ; os << ( char ) 217 << "\n" ; } Class Matrix void Matrix :: output ( ofstream & os ) const { os.setf ( ios :: showpoint ) ; os.setf ( ios :: fixed , ios :: floatfield ) ; os << numRows << " " << numCols << "\n" ; for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < numCols ; j ++ ) os << setw ( 6 ) << setprecision ( 2 ) << elements [ i ] [ j ] ; os << "\n" ; } } Class Matrix const Matrix & Matrix :: input ( istream & is ) { cout << "Input Matrix size: " << numRows << " rows by " << numCols << " columns \n" ; for ( int i = 0 ; i < numRows ; i ++ ) { cout << "Please enter " << numCols << " values separated by spaces for row no." << i+1 << ": " ; for ( int j = 0 ; j < numCols ; j ++ ) { cin >> elements [ i ] [ j ] ; } } return * this ; } const Matrix & Matrix :: input ( ifstream & is ) { int Rows , Cols ; is >> Rows ; is >> Cols ; if ( Rows > 0 && Cols > 0 ) { Matrix temp ( Rows , Cols ) ; * this = temp ; for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < numCols ; j ++ ) { is >> elements [ i ] [ j ] ; } } } return * this ; } Class Matrix const Matrix & Matrix :: transpose ( ) { if ( numRows == numCols ) // Square Matrix { double temp ; for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = i + 1 ; j < numCols ; j ++ ) { temp = elements [ i ] [ j ]; elements [ i ] [ j ] = elements [ j ] [ i ] ; elements [ j ] [ i ] = temp ; } } } Class Matrix else { Class Matrix Matrix temp(numCols, numRows); for ( int i = 0 ; i < numRows ; i ++ ) { for ( int j = 0 ; j < numCols ; j ++ ) { temp.elements [ j ] [ i ] = elements [ i ] [ j ] ; } } * this = temp ; } return * this ; } ...
View Full Document

Ask a homework question - tutors are online