Operator Overloading
Operator Overloading
Learning outcomes
Upon completing this topic you should be able to:
Operator Overloading
Operator overloading is a mechanism of making user-defined data types to behavior
like built-in types. For example, using in-build data types, line 3 below is legal.
int x=10; //line 1
int y=20; //line 2
int z=x+y; //line 3
However, using user-defined data types, line 3 below cannot be understood by the
compiler.
Rectangle rect1(4, 6); //line 1
Rectangle rect2(5, 9); //line 2
Rectangle rect3=rect1+rect2; //line 3
Line 3 will be compiled successfully only if plus(+) operator is overloaded. If the
plus(+) operator is overloaded we can be able to add two objects as shown below.
Rectangle rect1(10);
Rectangle rect2(30);
Rectangle rect3=rect1+rect2;
1
c) ?:
d) sizeof
An operator overloading function may be either a member function or a non-member
function.
Non-member operator overloading functions are made to be friend function so that
they can access data members of other classes.
And since the function is a member of Rectangle class, we have to call it using an
2
object that has been created from Rectangle class. Note also that operator+ function
must return a value which has to be assigned to rect3 variable.
This value is an object of type Rectangle.
Note that the compiler cannot interpret line 3 as shown below
Rectangle rect3=rect2.operator+(rect1);
Let us now write a complete program that as our operator+() function defined.
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
R e c t a n g l e . cpp T h i s program d e m o n s t r a t e s how b i n a r y
operators are overloaded
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ∗/
# i n c l u d e < i o s t r e a m . h>
cl a s s Rectangle
{
private :
i nt length ;
public :
Rectangle ( i n t ) ;
vo i d p r i n t _ v a l u e ( v o i d ) ;
R e c t a n g l e o p e r a t o r +( R e c t a n g l e ) ;
};
/ / defining constructor outside
the class declar ation
Rectangle : : Rectangle ( i n t l )
{ length=l ;
}
/ / defining operator overloading
function outside the class decla ration
R e c t a n g l e R e c t a n g l e : : o p e r a t o r +( R e c t a n g l e r )
{
Rectangle r e c t;
r e c t . len gth = len gt h +r . len gth ; r e t u r n r e c t;
}
vo i d R e c t a n g l e : : p r i n t _ v a l u e ( v o i d )
{
3
cout <<The l e n g t h i s : < < l e n g t h << e n d l ;
}
i n t main ( v o i d )
{
Rectangle rect1 ( 6 ) ;
Rectangle rect2 ( 7 ) ;
Rectangle rect3 = rect1 + rect2 ;
rect1 . print_value ( ) ;
rect2 . print_value ( ) ;
rect3 . print_value ( ) ;
return 0;
}
If you compile and execute this program, it will print the following on the screen
The l e n g t h i s 6
The l e n g t h i s 7
The l e n g t h i s 13
Note that the syntax of defining an operator overloading function is the same as the
syntax of defining other member functions.
4
using non member function.
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
R e c t a n g l e . cpp T h i s program d e m o n s t r a t e s how b i n a r y
operators are overloaded
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
∗ / # i n c l u d e < i o s t r e a m . h>
c l a s s Rectangle
{
private :
i nt length ;
public :
Rectangle ( i n t ) ;
vo i d p r i n t _ v a l u e ( v o i d ) ;
f r i e n d R e c t a n g l e o p e r a t o r +( R e c t a n g l e ) ;
}; / / defining constructor outside
the class declar ation
Rectangle : : Rectangle ( i n t l )
{ length=l ;
}
/ / defining operator overloading function
outside the cl ass decl arat ion
R e c t a n g l e o p e r a t o r +( R e c t a n g l e r 1 , R e c t a n g l e r 2 )
{
Rectangle r e c t;
r e c t . len g th =r1 . le ngth +r2 . l en gth ;
r e t u r n r e c t;
}
vo i d R e c t a n g l e : : p r i n t _ v a l u e ( v o i d )
{
cout <<The l e n g t h i s : < < l e n g t h << e n d l ;
}
i n t main ( v o i d )
{
Rectangle rect1 ( 6 ) ;
5
Rectangle rect2 ( 7 ) ;
Rectangle rect3 = rect1 + rect2 ;
rect1 . print_value ( ) ;
rect2 . print_value ( ) ;
rect3 . print_value ( ) ;
return 0;
}
If you compile and execute this program, it will produce the following output on
the screen
The l e n g t h i s 6
The l e n g t h i s 7
The l e n g t h i s 13
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
R e c t a n g l e . cpp T h i s program d e m o n s t r a t e s how b i n a r y
operators are overloaded
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
∗ / # i n c l u d e < i o s t r e a m . h>
c l a s s Rectangle
{
private :
i nt length ;
public :
Rectangle ( i n t ) ;
vo i d p r i n t _ v a l u e ( v o i d ) ;
Rectangle operato r ++();
}; / / defining constructor outside the class declaration
Rectangle : : Rectangle ( i n t l )
{ length=l ;
}
/ / defining operator overloading function
outside the c l a s s d e c l a r a t i o n Rectangle Rectangle
: : o p e r a t o r ++()
{
l e n g t h ++;
return ∗ this ;
}
vo i d R e c t a n g l e : : p r i n t _ v a l u e ( v o i d )
{
cout <<" The l e n g t h i s :" < < l e n g t h << e n d l ;
}
i n t main ( v o i d )
{
Rectangle rect1 ( 6 ) ;
R e c t a n g l e r e c t 2 =++ r e c t 1 ;
rect1 . print_value ( ) ;
7
rect2 . print_value ( ) ;
return 0;
}
If you compile and execute this program, it will output the following on the screen.
The length is 7
The length is 7
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
R e c t a n g l e . cpp T h i s program d e m o n s t r a t e s
how b i n a r y o p e r a t o r s a r e o v e r l o a d e d
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
∗ / # i n c l u d e < i o s t r e a m . h>
c l a s s Rectangle
{
private :
int length ;
public :
8
Rectangle ( i n t ) ;
vo i d p r i n t _ v a l u e ( v o i d ) ;
f r i e n d Rectangle o pe r a t o r ++ ( );
};
/ / defining constructor outside the cl ass declaration
Rectangle : : Rectangle ( i n t l )
{
length=l ;
}
/ / defining operator overloading function
outside the cl ass decl arat ion
R e c t a n g l e o p e r a t o r ++( R e c t a n g l e r e c t )
{
( r e c t . length )++;
return ∗ rect ;
}
vo i d R e c t a n g l e : : p r i n t _ v a l u e ( v o i d )
{
cout <<" The l e n g t h i s :" < < l e n g t h << e n d l ;
}
i n t main ( v o i d )
{
Rectangle rect1 ( 6 ) ;
R e c t a n g l e r e c t 2 =++ r e c t 1 ;
rect1 . print_value ( ) ;
rect2 . print_value ( ) ;
return 0;
}
If you compile and execute this program, it will output the following on the screen.
The length is 7
The length is 7
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
R e c t a n g l e . cpp T h i s program d e m o n s t r a t e s
how b i n a r y o p e r a t o r s a r e o v e r l o a d e d
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
∗ / # i n c l u d e < i o s t r e a m . h>
c l a s s Rectangle
{
private :
int length ;
public :
Rectangle ( i n t ) ;
vo i d p r i n t _ v a l u e ( v o i d ) ;
R e c t a n g l e o p e r a t o r ++( i n t ) ;
};
11
square1 and zero (0) are values that are sent to operator++() function. Zero (0) is
used to distinguish between pre and post decrement/increment operator overload-
ing.
Zero (0) has no other use.
Below is an example of a program that shows how post-increment operation is
overloaded using a non-member function
/∗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
R e c t a n g l e . cpp T h i s program d e m o n s t r a t e s how
unary o p e r a t o r
i s overloaded
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
∗ / # i n c l u d e < i o s t r e a m . h>
c l a s s Rectangle
{
private :
int length ;
public :
Rectangle ( i n t ) ;
vo i d p r i n t _ v a l u e ( v o i d ) ;
f r i e n d R e c t a n g l e o p e r a t o r ++( R e c t a n g l e , i n t );
};
/ ∗ −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
o v e r l o a d . cpp Th i s program d e m o n s t r a t e s
how t o o v e r l o a d i n s e r t i o n ( < <)
and e x t r a c t i o n ( > >) o p e r a t o r s
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
∗/
\ # i n c l u d e < i o s t r e a m . h>
cl a s s Rectangle
{
private :
int length ;
public :
Rectangle ( i n t ) ;
f r i e n d o s t r e a m& o p e r a t o r <<
( o s t r e a m& o u t p u t , R e c t a n g l e r e c t 1 ) ;
f r i e n d i s t r e a m& o p e r a t o r >>
( i s t r e a m& i n p u t , R e c t a n g l e r e c t 2 ) ;
};
/ / defining constructor outside the cl ass declaration
Rectangle : : Rectangle ( i n t l )
{
length=l ;
}
/ / defining operator overloading
function outside the class declaration
o s t r e a m& o p e r a t o r s < <( o s t r e a m& o u t p u t ,
Rectangle rect1 )
{
14
o u t p u t << r e c t 1 . l e n g t h << e n d l ; r e t u r n o u t p u t ;
}
i s t r e a m& o p e r a t o r s < <( i s t r e a m& i n p u t , R e c t a n g l e r e c t 2 )
{
r e c t 2 >> r e c t 2 . l e n g t h ;
return input ;
}
i n t main ( v o i d )
{
Rectangle rect1 ( 6 ) ;
cout <<The v a l u e i s << r e c t 1 << e n d l ;
cout << E n t e r a v a l u e : ;
c in >> r e c t 1 cout <<The v a l u e i s << r e c t 1 << e n d l ;
return 0;
}
If you compile and execute this program, the following will be displayed on the
computer screen.
The value is 6 Enter a value: 10 The value is 10
Revision Questions
Example ✐.
Suppose that the operator << is to be overloaded for a user-defined class called
mystery. Why must << operator be overloaded using a friend operator overloading
function
Solution: The leftmost operand of << is an object of type ostream, and therefore
since the class is of type mystery an object of type ostream can not be used to call
the operator overloading function. This object has to be passed to the function as an
argument. In that case the operator overlaoding function must be a friend operator
overlaoding function Q
16
References and Additional Reading Materials
• David Flanagan, 2005, Java in a Nutshell (5th Edition), O’Reilly Press,
17