cs201- introduction to programming- lecture 45

Post on 18-Dec-2014

49 Views

Category:

Education

6 Downloads

Preview:

Click to see full reader

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 45 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

TRANSCRIPT

Introduction to Introduction to ProgrammingProgramming

Lecture 45Lecture 45

Assignment Operator

A = B

Class MatrixClass Matrix

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

Assignment Operator

( A = B ) = C

Self Assignment

A = A

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 ] ; }}

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 ;}}

Addition Operator+

Addition Operator

A + BBoth A and B are Matrices

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

+= Operator

+= Operator

A += B

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 ;

}}

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 ;}}

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

* Operator

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 ;}}

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 ;}}

Matrix / dCheck d = 0 ?

>> Extraction Operator

<< Insertion Operator

cin >> m

Where m is an object of the class Matrix

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 ;

}}

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 ;

}}

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 ;}}

main ( )main ( )

{{

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

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

……… ………

}}

Template for Matrix Class

Rules for Programming

• Sequences• Decisions• Loops

Variable

Variable is a name for a value

PointerPointerPointer is the Pointer is the address address

of a location in the of a location in the

memorymemory

Arrays

DecisionsDecisionsif ( condition ){

statement ( s ) ;}

Orif ( condition ){

statement ( s ) ;}else{

statement ( s ) ;}

Orif ( condition ){

statement ( s ) ;}else if ( condition )

{statement ( s ) ;

}

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

Classes Classes and and

ObjectsObjects

Classes and ObjectsClasses and Objects

Object.memberObject.member

Garbage Garbage CollectioCollectio

nn

Truth Truth TableTable

Structured Query Structured Query LanguageLanguage

SQLSQL

Optimizer Optimizer

top related