cs201- introduction to programming- lecture 45
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 45 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 45Lecture 45
![Page 2: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/2.jpg)
Assignment Operator
A = B
![Page 3: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/3.jpg)
Class MatrixClass Matrix
const Matrix & operator = ( const Matrix & const Matrix & operator = ( const Matrix & m ) ; m ) ;
![Page 4: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/4.jpg)
Assignment Operator
( A = B ) = C
![Page 5: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/5.jpg)
Self Assignment
A = A
![Page 6: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/6.jpg)
Class MatrixClass Matrixconst Matrix & Matrix :: operator = ( const Matrix & m )const Matrix & Matrix :: operator = ( const Matrix & m ){ { if ( &m != this)if ( &m != this) {{ if ( numRows != m.numRows || numCols != m.numCols )if ( numRows != m.numRows || numCols != m.numCols ) {{ delete [ ] elements ;delete [ ] elements ; elements = new ( double * ) [ m.numRows ] ;elements = new ( double * ) [ m.numRows ] ; for ( int i = 0 ; i < m.numRows ; i ++ )for ( int i = 0 ; i < m.numRows ; i ++ ) elements [ i ] = new double [ m.numCols ] ;elements [ i ] = new double [ m.numCols ] ; }}
![Page 7: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/7.jpg)
Class Matrix Class Matrix numRows = m.numRows ;numRows = m.numRows ; numCols = m.numCols ;numCols = m.numCols ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ for ( int j = 0 ; j < numCols ; j ++ ) for ( int j = 0 ; j < numCols ; j ++ ) {{ elements [ i ] [ j ] = m.elements [ i ] [ j ] ;elements [ i ] [ j ] = m.elements [ i ] [ j ] ; }} }} }} return * this ;return * this ;}}
![Page 8: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/8.jpg)
Addition Operator+
![Page 9: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/9.jpg)
Addition Operator
A + BBoth A and B are Matrices
![Page 10: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/10.jpg)
Class MatixClass MatixMatrix Matrix :: operator + ( Matrix & m ) constMatrix Matrix :: operator + ( Matrix & m ) const{ { // Check for conformability// Check for conformability if ( numRows == m.numRows && numCols == m.numCols )if ( numRows == m.numRows && numCols == m.numCols ) {{ Matrix temp ( * this ) ;Matrix temp ( * this ) ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ ) {{ for ( int j = 0 ; j < numCols ; j ++)for ( int j = 0 ; j < numCols ; j ++) {{ temp.elements [ i ] [ j ] += m.elements [ i ] [ j ] ;temp.elements [ i ] [ j ] += m.elements [ i ] [ j ] ; }} }} return temp ;return temp ; }}
Matrix temp ( * this ) ;Matrix temp ( * this ) ;return temp return temp ;;
}}8:50
![Page 11: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/11.jpg)
+= Operator
![Page 12: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/12.jpg)
+= Operator
A += B
![Page 13: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/13.jpg)
Class Matrix Class Matrix
const Matrix & Matrix :: operator += ( Matrix & m )const Matrix & Matrix :: operator += ( Matrix & m )
{{
* this = * this + m ;* this = * this + m ;
return * this ;return * this ;
}}
![Page 14: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/14.jpg)
Class MatrixClass MatrixMatrix Matrix :: operator + ( double d ) constMatrix Matrix :: operator + ( double d ) const{ { Matrix temp ( * this ) ;Matrix temp ( * this ) ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ ) {{ for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ ) {{ temp.elements [ i ] [ j ] += d ;temp.elements [ i ] [ j ] += d ; }} }} return temp ;return temp ;}}
![Page 15: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/15.jpg)
Class MatrixClass MatrixMatrix operator + ( double d , Matrix & m )Matrix operator + ( double d , Matrix & m ){{ Matrix temp ( m ) ;Matrix temp ( m ) ; for ( int i = 0 ; i < temp.numRows ; i ++ ) for ( int i = 0 ; i < temp.numRows ; i ++ )
{{ for ( int j = 0; j < temp.numCols ; j ++ )for ( int j = 0; j < temp.numCols ; j ++ )
{{ temp.elements [ i ] [ j ] += d ;temp.elements [ i ] [ j ] += d ; }} }} return temp ;return temp ;}} 13:00
![Page 16: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/16.jpg)
* Operator
![Page 17: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/17.jpg)
Class MatrixClass MatrixMatrix Matrix :: operator * ( const Matrix & m )Matrix Matrix :: operator * ( const Matrix & m ){ { Matrix temp ( numRows , m.numCols ) ;Matrix temp ( numRows , m.numCols ) ; if ( numCols == m.numRows ) if ( numCols == m.numRows ) {{ for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ for ( int j = 0 ; j < m.numCols ; j ++ )for ( int j = 0 ; j < m.numCols ; j ++ )
{{ temp.elements [ i ] [ j ] = 0.0 ;temp.elements [ i ] [ j ] = 0.0 ; for ( int k = 0 ; k < numCols ; k ++ )for ( int k = 0 ; k < numCols ; k ++ )
{{ temp.elements [ i ] [ j ] += elements [ i ] [ k ] * m.elements [ k ] [ j ] ;temp.elements [ i ] [ j ] += elements [ i ] [ k ] * m.elements [ k ] [ j ] ; }} }} }} }} return temp ;return temp ;}}
![Page 18: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/18.jpg)
Class MatrixClass MatrixMatrix Matrix :: operator * ( double d ) constMatrix Matrix :: operator * ( double d ) const{ { Matrix temp ( * this ) ;Matrix temp ( * this ) ; for ( int i = 0 ; i < numRows ; i ++ )for ( int i = 0 ; i < numRows ; i ++ )
{{ for ( int j = 0 ; j < numCols ; j ++ )for ( int j = 0 ; j < numCols ; j ++ ) {{ temp.elements [ i ] [ j ] *= d ;temp.elements [ i ] [ j ] *= d ; }} }} return temp ;return temp ;}}
![Page 19: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/19.jpg)
Matrix / dCheck d = 0 ?
![Page 20: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/20.jpg)
>> Extraction Operator
<< Insertion Operator
![Page 21: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/21.jpg)
cin >> m
Where m is an object of the class Matrix
![Page 22: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/22.jpg)
Class MatrixClass Matrix
istream & operator >> ( istream & is , Matrix & istream & operator >> ( istream & is , Matrix & m )m )
{{
m.input ( is ) ;m.input ( is ) ;
return is ;return is ;
}}
![Page 23: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/23.jpg)
Class MatrixClass Matrix
ifstream & operator >> ( ifstream & is , Matrix & ifstream & operator >> ( ifstream & is , Matrix & m )m )
{{
m.input ( is ) ;m.input ( is ) ;
return is ;return is ;
}}
![Page 24: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/24.jpg)
Class MatrixClass Matrixostream & operator << ( ostream & os , Matrix & m )ostream & operator << ( ostream & os , Matrix & m ){{ m.output ( ) ;m.output ( ) ; return os ;return os ;}}
ofstream & operator << ( ofstream & os , Matrix & m )ofstream & operator << ( ofstream & os , Matrix & m ){{ m.output ( os ) ;m.output ( os ) ; return os ;return os ;}}
![Page 25: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/25.jpg)
main ( )main ( )
{{
Matrix m ( 3 , 3 ) ;Matrix m ( 3 , 3 ) ;
m.output ( ) ;m.output ( ) ;
……… ………
}}
![Page 26: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/26.jpg)
Template for Matrix Class
![Page 27: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/27.jpg)
Rules for Programming
• Sequences• Decisions• Loops
![Page 28: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/28.jpg)
Variable
Variable is a name for a value
![Page 29: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/29.jpg)
PointerPointerPointer is the Pointer is the address address
of a location in the of a location in the
memorymemory
![Page 30: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/30.jpg)
Arrays
![Page 31: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/31.jpg)
DecisionsDecisionsif ( condition ){
statement ( s ) ;}
Orif ( condition ){
statement ( s ) ;}else{
statement ( s ) ;}
Orif ( condition ){
statement ( s ) ;}else if ( condition )
{statement ( s ) ;
}
![Page 32: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/32.jpg)
Repetition Repetition StructureStructure
There are three kinds of Loops in C Language :There are three kinds of Loops in C Language :– while while – do – while do – while – forfor
![Page 33: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/33.jpg)
Classes Classes and and
ObjectsObjects
![Page 34: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/34.jpg)
Classes and ObjectsClasses and Objects
Object.memberObject.member
![Page 35: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/35.jpg)
Garbage Garbage CollectioCollectio
nn
![Page 36: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/36.jpg)
Truth Truth TableTable
![Page 37: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/37.jpg)
Structured Query Structured Query LanguageLanguage
SQLSQL
![Page 38: CS201- Introduction to Programming- Lecture 45](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e738b4795900548b45ec/html5/thumbnails/38.jpg)
Optimizer Optimizer