0% found this document useful (0 votes)
5 views

Introduction To Programming - CS201 Power Point Slides Lecture 45

Uploaded by

Hasnain ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Introduction To Programming - CS201 Power Point Slides Lecture 45

Uploaded by

Hasnain ullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Introduction to

Programming
Lecture 45
Assignment Operato

A=B
Class Matrix

const Matrix & operator = ( const Matrix &


m);
Assignment Operato

(A=B)=C
Self Assignment

A=A
Class Matrix
const Matrix & Matrix :: operator = ( const Matrix & m )
{
if ( &m != this)
{
if ( numRows != m.numRows || numCols != m.numCo
{
delete [ ] elements ;
elements = new ( double * ) [ m.numRows ] ;
for ( int i = 0 ; i < m.numRows ; i ++ )
elements [ i ] = new double [ m.numCols ] ;
}
Class Matrix
numRows = m.numRows ;
numCols = m.numCols ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = m.elements [ i ] [ j ] ;
}
}
}
return * this ;
}
Addition Operato
+
Addition Operator

A+B
Both A and B are Matrices
Class Matix
Matrix Matrix :: operator + ( Matrix & m ) const
{
// Check for conformability
if ( numRows == m.numRows && numCols == m.numCols )
{
Matrix temp ( * this ) ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++)
{
temp.elements [ i ] [ j ] += m.elements [ i ] [ j ] ;
}
}
return temp ;
}
Matrix temp ( * this ) ;
return temp ;
}
8:50
+= Operator
+= Operator

A += B
Class Matrix
const Matrix & Matrix :: operator += ( Matrix &
{
* this = * this + m ;
return * this ;
}
Class Matrix
Matrix Matrix :: operator + ( double d ) const
{
Matrix temp ( * this ) ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
temp.elements [ i ] [ j ] += d ;
}
}
return temp ;
}
Class Matrix
Matrix operator + ( double d , Matrix & m )
{
Matrix temp ( m ) ;
for ( int i = 0 ; i < temp.numRows ; i ++ )
{
for ( int j = 0; j < temp.numCols ; j ++ )
{
temp.elements [ i ] [ j ] += d ;
}
}
return temp ;
} 13:00
*
Operator
Class Matrix
Matrix Matrix :: operator * ( const Matrix & m )
{
Matrix temp ( numRows , m.numCols ) ;
if ( numCols == m.numRows )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < m.numCols ; j ++ )
{
temp.elements [ i ] [ j ] = 0.0 ;
for ( int k = 0 ; k < numCols ; k ++ )
{
temp.elements [ i ] [ j ] += elements [ i ] [ k ] * m.elements [ k ]
}
}
}
}
return temp ;
}
Class Matrix
Matrix Matrix :: operator * ( double d ) const
{
Matrix temp ( * this ) ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
temp.elements [ i ] [ j ] *= d ;
}
}
return temp ;
}
Matrix / d
Check d = 0 ?
<< Insertion Operator
>> Extraction Operato
Where m is an object of the class Matrix

cin >> m
Class Matrix
istream & operator >> ( istream & is , Matrix
m)
{
m.input ( is ) ;
return is ;
}
Class Matrix

ifstream & operator >> ( ifstream & is , Matrix


m)
{
m.input ( is ) ;
return is ;
}
Class Matrix
ostream & operator << ( ostream & os , Matrix & m )
{
m.output ( ) ;
return os ;
}

ofstream & operator << ( ofstream & os , Matrix & m )


{
m.output ( os ) ;
return os ;
}
main ( )
{
Matrix m ( 3 , 3 ) ;
m.output ( ) ;
………
}
Template for Matrix
Class
Rules for Programming
• Sequences
• Decisions
• Loops
Variable

Variable is a name
for a value
Pointer
Pointer is the
address
of a location in the
memory
Arrays
Decisions Or
if ( condition ) if ( condition )
{ {
statement ( s ) ; statement ( s ) ;
} }
Or else if ( condition )
if ( condition )
{ {
statement ( s ) ; statement ( s ) ;
} }
else
{
statement ( s ) ;
}
Repetition
Structure
There are three kinds of Loops in C Langu
– while
– do – while
– for
Classes
and
Objects
Classes and Objects

Object.member
Garbage
Collectio
n
Truth
Table
Structured Query
Language

SQL
Optimizer

You might also like