CS201- Introduction to Programming- Lecture 37
-
Upload
bilal-ahmed -
Category
Education
-
view
20 -
download
1
description
Transcript of CS201- Introduction to Programming- Lecture 37
Introduction to Introduction to ProgrammingProgrammingLecture 37Lecture 37
Operator Operator OverloadinOverloadin
gg
Stream Insertion / Stream Insertion / ExtractionExtraction
Operator Operator OverloadingOverloading
int i ;int i ;cin >> i ;cin >> i ;
int a , b , c ;int a , b , c ;
a + b + c ; a + b + c ;
cin >> i >> j ;cin >> i >> j ;
Friend Friend FunctionFunction
Member Member OperatorOperator
ss
Object.dataObject.dataObject.function Object.function ( )( )
Stream Insertion Stream Insertion OperatorOperator
int i = 5 , j = 10 , k = int i = 5 , j = 10 , k = 15 ; 15 ;
cout << i << j << k ; cout << i << j << k ;
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
} }
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
cout << d ;cout << d ;
cout << “The description of the vehicle is \n” << cout << “The description of the vehicle is \n” << d ;d ;
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 ) ;
} ;} ;
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 ;}}
int i ;int i ;
cin >> i ; cin >> i ;
Matrix x ;Matrix x ;cin >> x ; cin >> x ;
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 ) ;
};};
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;}}
ExampleExample
Matrix m ;Matrix m ;m.getMatrix ( ) ;m.getMatrix ( ) ;m.displayMatrix ( ) ;m.displayMatrix ( ) ;
ExampleExample
Matrix m ;Matrix m ;
cin >> m ;cin >> m ;
cout << m ;cout << m ;