















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of operator overloading and stream insertion/extraction in c++. It includes examples of overloading the '<<' operator for a 'vehicle' class and a 'matrix' class, as well as code snippets demonstrating the use of these operators. Students will learn how to define and implement operator functions, and how to use these operators to input and output data.
Typology: Slides
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Operator Overloading
int i ; cin >> i ;
int a , b , c ; a + b + c ;
Friend Function
Member Operators
Stream Insertion Operator
int i = 5 , j = 10 , k = 15 ; cout << i << j << k ;
ostream & operator << ( ostream & output , vehicle d ) { output<< d.seats ; output<<d.tires ;
return output ; } Definition
cout << d ;
Example class Matrix { private : int rows , cols ; int elements [ 3 ] [ 3 ] ; public : Matrix ( int rows = 3 , int cols = 3 ) ; friend ostream & operator << ( ostream & output , Matrix m ) ; } ;
Example ostream& operator << ( ostream & output , Matrix m ) { for ( int i = 0 ; i < m.rows ; i ++ ) { for ( int j = 0 ; j < m.cols ; j ++ ) { output << m.elements [ i ] [ j ] ; } } return output ; }
Matrix x ; cin >> x ;
Example class Matrix { private : int rows, cols ; int elements [ 3 ] [ 3 ] ; public : Matrix ( int rows = 3 , int cols = 3 ) ; friend ostream & operator << ( ostream & output , Matrix m ) ; friend istream & operator >> ( istream & input , Matrix m ) ; };