0% found this document useful (0 votes)
42 views23 pages

CS201 37

Operator overloading allows operators like +, -, <<, >> to be used with user-defined types like classes. This allows things like adding two objects of a class type or outputting an object to a stream. The lecture discusses stream insertion and extraction operators << and >>. It provides an example of overloading these operators for a Matrix class to allow input and output of matrix objects to streams using cin >> and cout <<. Friend functions are used to define the overloaded operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views23 pages

CS201 37

Operator overloading allows operators like +, -, <<, >> to be used with user-defined types like classes. This allows things like adding two objects of a class type or outputting an object to a stream. The lecture discusses stream insertion and extraction operators << and >>. It provides an example of overloading these operators for a Matrix class to allow input and output of matrix objects to streams using cin >> and cout <<. Friend functions are used to define the overloaded operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to Programming

Lecture 37
Operator
Overloading
Operator Overloading

Stream Insertion /
Extraction
int i ;
cin >> i ;
int a , b , c ;
a+b+c;
cin >> i >> j ;
Friend
Function
Member
Operators
Object.data
Object.function ( )
Stream Insertion Operator
int i = 5 , j = 10 , k = 15 ;
cout << i << j << k ;
Return type:
Reference to the output stream
Operator

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

Reference to the output Object of the


stream class
Definition
ostream & operator << ( ostream & output , vehicle d )
{
output<< d.seats ;
output<<d.tires ;
-------
return output ;
}
cout << d ;
cout << “The description of the vehicle is \n” << 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 ;
}
int i ;
cin >> i ;
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 ) ;
};
Example
istream & operator >> ( istream & input , Matrix & m )
{
cout<< “Please enter the values of the matrix” ;
    for ( int i = 0 ; i < m.rows ; i ++ )
    {
        for ( int j = 0 ; j < m.cols ; j ++ )
        {
          cout << “Please enter the values of elements ” << i << “,” << j ;
input >> m.elements [ i ] [ j ] ;
        }
    }
    return input;
}
Example

Matrix m ;
m.getMatrix ( ) ;
m.displayMatrix ( ) ;
Example
Matrix m ;
cin >> m ;
cout << m ;

You might also like