![Page 1: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgrammingLecture 37Lecture 37
![Page 2: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/2.jpg)
Operator Operator OverloadinOverloadin
gg
![Page 3: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/3.jpg)
Stream Insertion / Stream Insertion / ExtractionExtraction
Operator Operator OverloadingOverloading
![Page 4: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/4.jpg)
int i ;int i ;cin >> i ;cin >> i ;
![Page 5: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/5.jpg)
int a , b , c ;int a , b , c ;
a + b + c ; a + b + c ;
![Page 6: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/6.jpg)
cin >> i >> j ;cin >> i >> j ;
![Page 7: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/7.jpg)
Friend Friend FunctionFunction
![Page 8: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/8.jpg)
Member Member OperatorOperator
ss
![Page 9: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/9.jpg)
Object.dataObject.dataObject.function Object.function ( )( )
![Page 10: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/10.jpg)
Stream Insertion Stream Insertion OperatorOperator
![Page 11: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/14.jpg)
cout << d ;cout << d ;
![Page 15: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/17.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/18.jpg)
int i ;int i ;
cin >> i ; cin >> i ;
![Page 19: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/19.jpg)
Matrix x ;Matrix x ;cin >> x ; cin >> x ;
![Page 20: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/20.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/21.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/22.jpg)
ExampleExample
Matrix m ;Matrix m ;m.getMatrix ( ) ;m.getMatrix ( ) ;m.displayMatrix ( ) ;m.displayMatrix ( ) ;
![Page 23: CS201- Introduction to Programming- Lecture 37](https://reader033.vdocument.in/reader033/viewer/2022051514/5492e746b479596e4f8b460b/html5/thumbnails/23.jpg)
ExampleExample
Matrix m ;Matrix m ;
cin >> m ;cin >> m ;
cout << m ;cout << m ;