Introduction to Programming
Lecture 44
Class Matrix
class Matrix
{
private :
int numRows , numCols ;
double ** elements ;
};
Class Matrix
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 ;
// 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 ;
d is a variable
‘A’ is an object of a of type double
class Matrix
A+d;
d is a ‘a’ is an
variable of object of a
type double class Matrix
d+A;
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
d is a variable
‘A’ is an
of type double
object of a
class Matrix
A–d;
d is a ‘a’ is an
variable of object of a
type double class Matrix
d–A;
Class Matrix
// Minus Operator
Matrix operator - ( Matrix & m ) const ;
Matrix operator - ( double d ) const ;
friend Matrix operator - ( double d , Matrix & m ) ;
A*B;
Where A and B are both matrices
‘A’ is an d is a variable
object of a of type double
class Matrix
A*d;
d is a
variable of ‘a’ is an
type double object of a
class Matrix
d*A;
Class Matrix
// Multiplication Operator
Matrix operator * ( const Matrix & m ) ;
Matrix operator * ( double d ) const ;
friend Matrix operator * ( const double d , const Matrix & m ) ;
‘A’ is an d is a variable
object of a of type double
class Matrix
A/d;
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 ) ;
Class Matrix
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 ;
}
}
Matrix A ( B ) ;
Matrix A = B ;
Class Matrix
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
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 ;
}
Class Matrix
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
{
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 ;
}