CS201- Introduction to Programming- Lecture 37

23
Introduction to Programmin Introduction to Programmin Lecture 37 Lecture 37

TAGS:

description

Virtual University Course CS201- Introduction to Programming Lecture No 37 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 37

Page 1: CS201- Introduction to Programming- Lecture 37

Introduction to Introduction to ProgrammingProgrammingLecture 37Lecture 37

Page 2: CS201- Introduction to Programming- Lecture 37

Operator Operator OverloadinOverloadin

gg

Page 3: CS201- Introduction to Programming- Lecture 37

Stream Insertion / Stream Insertion / ExtractionExtraction

Operator Operator OverloadingOverloading

Page 4: CS201- Introduction to Programming- Lecture 37

int i ;int i ;cin >> i ;cin >> i ;

Page 5: CS201- Introduction to Programming- Lecture 37

int a , b , c ;int a , b , c ;

a + b + c ; a + b + c ;

Page 6: CS201- Introduction to Programming- Lecture 37

cin >> i >> j ;cin >> i >> j ;

Page 7: CS201- Introduction to Programming- Lecture 37

Friend Friend FunctionFunction

Page 8: CS201- Introduction to Programming- Lecture 37

Member Member OperatorOperator

ss

Page 9: CS201- Introduction to Programming- Lecture 37

Object.dataObject.dataObject.function Object.function ( )( )

Page 10: CS201- Introduction to Programming- Lecture 37

Stream Insertion Stream Insertion OperatorOperator

Page 11: CS201- Introduction to Programming- Lecture 37

int i = 5 , j = 10 , k = int i = 5 , j = 10 , k = 15 ; 15 ;

cout << i << j << k ; cout << i << j << k ;

Page 12: CS201- Introduction to Programming- Lecture 37

ostream & operator << ( ostream & output , vehicle ostream & operator << ( ostream & output , vehicle A )A )

Reference to the output stream

Return type:Reference to the output stream

Object of the class

Operator

} }

Page 13: CS201- Introduction to Programming- Lecture 37

ostream & operator << ( ostream & output , ostream & operator << ( ostream & output , vehicle d )vehicle d )

{{output<< d.seats ;output<< d.seats ;

output<<d.tires ;output<<d.tires ;--------------

return output ;return output ;} }

Definition

Page 14: CS201- Introduction to Programming- Lecture 37

cout << d ;cout << d ;

Page 15: CS201- Introduction to Programming- Lecture 37

cout << “The description of the vehicle is \n” << cout << “The description of the vehicle is \n” << d ;d ;

Page 16: CS201- Introduction to Programming- Lecture 37

ExampleExampleclass Matrixclass Matrix{{

private :private :

                    int rows , cols ;int rows , cols ;          int elements [ 3 ] [ 3 ] ;           int elements [ 3 ] [ 3 ] ; public :public :

Matrix ( int rows = 3 , int cols = 3 ) ;Matrix ( int rows = 3 , int cols = 3 ) ;    friend ostream & operator << ( ostream & output , Matrix m ) ;friend ostream & operator << ( ostream & output , Matrix m ) ;

} ;} ;

Page 17: CS201- Introduction to Programming- Lecture 37

ExampleExample ostream& operator << ( ostream & output , Matrix m )ostream& operator << ( ostream & output , Matrix m ){{

for ( int i = 0 ; i < m.rows ; i ++ )for ( int i = 0 ; i < m.rows ; i ++ )        {{                for ( int j = 0 ; j < m.cols ; j ++ )for ( int j = 0 ; j < m.cols ; j ++ )                {{                    output << m.elements [ i ] [ j ] ;output << m.elements [ i ] [ j ] ;                }}        }}        return output ;return output ;}}

Page 18: CS201- Introduction to Programming- Lecture 37

int i ;int i ;

cin >> i ; cin >> i ;

Page 19: CS201- Introduction to Programming- Lecture 37

Matrix x ;Matrix x ;cin >> x ; cin >> x ;

Page 20: CS201- Introduction to Programming- Lecture 37

ExampleExampleclass Matrixclass Matrix{{

private :private :int rows, cols ;int rows, cols ;int elements [ 3 ] [ 3 ] ; int elements [ 3 ] [ 3 ] ;

public :public :Matrix ( int rows = 3 , int cols = 3 ) ;Matrix ( int rows = 3 , int cols = 3 ) ;friend ostream & operator << ( ostream & output , Matrix friend ostream & operator << ( ostream & output , Matrix

m ) ; m ) ;    friend istream & operator >> ( istream & input , Matrix m ) ;friend istream & operator >> ( istream & input , Matrix m ) ;

};};

Page 21: CS201- Introduction to Programming- Lecture 37

ExampleExample

istream & operator >> ( istream & input , Matrix & m )istream & operator >> ( istream & input , Matrix & m ){{ cout<< “Please enter the values of the matrix” ; cout<< “Please enter the values of the matrix” ;        for ( int i = 0 ; i < m.rows ; i ++ )for ( int i = 0 ; i < m.rows ; i ++ )    {    {        for ( int j = 0 ; j < m.cols ; j ++ )        for ( int j = 0 ; j < m.cols ; j ++ )        {        {          cout << “Please enter the values of elements ” << i << “,” <<           cout << “Please enter the values of elements ” << i << “,” << j ;j ;

input >> m.elements [ i ] [ j ] ;input >> m.elements [ i ] [ j ] ;        }        }    }    }    return input;    return input;}}

Page 22: CS201- Introduction to Programming- Lecture 37

ExampleExample

Matrix m ;Matrix m ;m.getMatrix ( ) ;m.getMatrix ( ) ;m.displayMatrix ( ) ;m.displayMatrix ( ) ;

Page 23: CS201- Introduction to Programming- Lecture 37

ExampleExample

Matrix m ;Matrix m ;

cin >> m ;cin >> m ;

cout << m ;cout << m ;