cs201- introduction to programming- lecture 45

38
Introduction to Introduction to Programming Programming Lecture 45 Lecture 45

Upload: bilal-ahmed

Post on 18-Dec-2014

46 views

Category:

Education


6 download

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

Introduction to Introduction to ProgrammingProgramming

Lecture 45Lecture 45

Page 2: CS201- Introduction to Programming- Lecture 45

Assignment Operator

A = B

Page 3: CS201- Introduction to Programming- Lecture 45

Class MatrixClass Matrix

const Matrix & operator = ( const Matrix & const Matrix & operator = ( const Matrix & m ) ; m ) ;

Page 4: CS201- Introduction to Programming- Lecture 45

Assignment Operator

( A = B ) = C

Page 5: CS201- Introduction to Programming- Lecture 45

Self Assignment

A = A

Page 6: CS201- Introduction to Programming- Lecture 45

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

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

Addition Operator+

Page 9: CS201- Introduction to Programming- Lecture 45

Addition Operator

A + BBoth A and B are Matrices

Page 10: CS201- Introduction to Programming- Lecture 45

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

+= Operator

Page 12: CS201- Introduction to Programming- Lecture 45

+= Operator

A += B

Page 13: CS201- Introduction to Programming- Lecture 45

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

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

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

* Operator

Page 17: CS201- Introduction to Programming- Lecture 45

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

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

Matrix / dCheck d = 0 ?

Page 20: CS201- Introduction to Programming- Lecture 45

>> Extraction Operator

<< Insertion Operator

Page 21: CS201- Introduction to Programming- Lecture 45

cin >> m

Where m is an object of the class Matrix

Page 22: CS201- Introduction to Programming- Lecture 45

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

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

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

main ( )main ( )

{{

Matrix m ( 3 , 3 ) ;Matrix m ( 3 , 3 ) ;

m.output ( ) ;m.output ( ) ;

……… ………

}}

Page 26: CS201- Introduction to Programming- Lecture 45

Template for Matrix Class

Page 27: CS201- Introduction to Programming- Lecture 45

Rules for Programming

• Sequences• Decisions• Loops

Page 28: CS201- Introduction to Programming- Lecture 45

Variable

Variable is a name for a value

Page 29: CS201- Introduction to Programming- Lecture 45

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

Arrays

Page 31: CS201- Introduction to Programming- Lecture 45

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

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

Classes Classes and and

ObjectsObjects

Page 34: CS201- Introduction to Programming- Lecture 45

Classes and ObjectsClasses and Objects

Object.memberObject.member

Page 35: CS201- Introduction to Programming- Lecture 45

Garbage Garbage CollectioCollectio

nn

Page 36: CS201- Introduction to Programming- Lecture 45

Truth Truth TableTable

Page 37: CS201- Introduction to Programming- Lecture 45

Structured Query Structured Query LanguageLanguage

SQLSQL

Page 38: CS201- Introduction to Programming- Lecture 45

Optimizer Optimizer