0% found this document useful (0 votes)
28 views17 pages

Operator Overloading

1) Operator overloading allows user-defined types to behave like built-in types with operators like + and -. It involves defining operator overloading functions. 2) Binary operators require two operands and can be overloaded using member or non-member functions. Member functions are called on the object being passed while non-member functions receive both operands as parameters. 3) Unary operators operate on a single operand. Increment/decrement operators can be overloaded as pre- or post-increment/decrement using member functions by defining functions like operator++().

Uploaded by

Kimani John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views17 pages

Operator Overloading

1) Operator overloading allows user-defined types to behave like built-in types with operators like + and -. It involves defining operator overloading functions. 2) Binary operators require two operands and can be overloaded using member or non-member functions. Member functions are called on the object being passed while non-member functions receive both operands as parameters. 3) Unary operators operate on a single operand. Increment/decrement operators can be overloaded as pre- or post-increment/decrement using member functions by defining functions like operator++().

Uploaded by

Kimani John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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;

Operation Overloading Function


In order to overload an operator, we must write a function to do that. Such a function
is referred to as an operator overloading function. The name of an operator
overloading function, is reserved word “operator” followed by the operator to over-
load. For example, to overload <=(less than or equal to ) operator we must write a
function with the following name: operator<=
The syntax for declaring an operator overloading function is
return-type operator operator-symbol(parameters);
Note the following operators cannot be overloaded:
a) .
b) .*

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.

Overloading Binary Operators


Binary operators are those operators that requires at least two operands.
For example:
+, -, and *
Let us now look an example of how to overload +(plus) operator

Overloading + operator using a member function


The name of the operator overloading function in this case is: operator+()
Recall operator is a reserved word and it is followed by the name of the operator
being overloaded, which is + in this case
Now let us assume we have the following statements:
Rectangle rect1(6); //line 1
Rectangle rect2(7); //line 2
Rectangle rect3=rect1+rect2; // line 3
In line 3, we are adding two objects i.e. rect1 and rect2 and the result is assigned to
variable rect3 of type Rectangle.
The two objects are of type Rectangle.
If our operator overloading function is a member of Rectangle class,
line 3 will be interpreted by the compiler as shown:
Rectangle rect3=rect1.operator+(rect2);
Note that on the right side of the assignment operator(=), we now have a message
operator+ being passed to rect1 object and the object is also being passed argument
rect2.
Ideally what we have is a call to a function called operator+() that is being passed
rect2 value.

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.

Overloading + operator using a non-member function


Let us now see how to overload + operator using a non- member function.
Since the function is a not a member function, it must be a friend function in order
for it to access data members.
Consider the three statements below.
Rectangle rect1(6); //line 1
Rectangle rect2(7); //line 2
Rectangle rect3=rect1+rect2; // line 3
Since we are overload + operator using a non-member function, the compiler will
interpret line 3 as shown below Rectangle rect3=operator+(rect1, rect2);
Note that now the two objects are passed as arguments to the operator overloading
function.
The operator overloading function returns a value of type Rectangle.

Let now write a complete program demonstrating how + operator is overloaded

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

Overloading Unary Operators


Unary operators are associated with only one operand. Increment and
decrement operators are examples of unary operators. These
operators are overloaded just like binary operators.
The overloading function can be either a member function or a non-member
functions.
Increment and decrement operators can be either pre-or post-decrement and
increment operators

Overloading pre-increment/decrement operators using member functions


Consider the statement shown below:
Square square1(20); //line 1
Square square2=++square1; //line 2
Since we are overloading ++ operator using a member function line 2 will be
interpreted by the compiler as follows: Square square2=square1.operator++();
What is on the right side of assignment operator (=) is a call to operator++()
function.

In an object-oriented program it is message passing. square1 is the object sent the


message, and operator++ is the message being passed.
Below is an example of a program that shows how pre-increment operation is over-
6
loaded using a 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 ) ;
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

Overloading pre-increment/decrement operators using a non-member function


Consider the statement shown below:
Square square1(20); //line 1
Square square2=++square1; //line 2
Since we are overloading ++ operator using a non-member function line 2 will be
interpreted by the compiler as follows: Square square2=operator++(square1); What
is on the right side of assignment operator (=) is a call to operator++() function.
In an object-oriented program it is referred to as message passing.
square1 is the object that is passed to the operator++() method.
Below is an example of a program that shows how pre-increment operation is over-
loaded 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 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

Overloading post-increment/decrement operators using member functions


Consider the statement shown below:
9
Square square1(20); //line 1
Square square2=square1++; //line 2
Since we are overloading ++ operator using a member function line 2 will be
interpreted by the compiler as follows: Square square2=square1.operator++(0);
What is on the right side of assignment operator (=) is a call to operator++()
function.
In an object-oriented program it is message passing. Zero (0) is a value that is sent
to operator++() function.
Zero (0) is an argument that is used to distinguish between pre and post
decrement/increment operators.
Zero (0) has no other use.
In fact it is referred to as a dummy parameter.
Below is an example of a program that shows how post-increment operation is
overloaded using a 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 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 ) ;
};

/ / defining constructor outside the cl ass declaration


Rectangle : : Rectangle ( i n t l )
{
length=l ;
}
10
/ / defining operator overloading function
outside the cl ass decl arat ion
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 ++( i n t dummy )
{
R e c t a n g l e temp =∗ t h i s ;
l e n g t h ++;
r e t u r n temp ;
}
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 is7
The length is 8

Overloading post-increment/decrement operators using non-member function


Consider the statement shown below:
Square square1(20); //line 1
Square square2=square1++; //line 2
Since we are overloading ++ operator using a non-member function line 2 will be
interpreted by the compiler as follows: Square square2=operator++(square1, 0);
What is on the right side of assignment operator (=) is a call to operator++()
function.
In an object-oriented program it is message passing.

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 );
};

/ / defining constructor outside the cl ass declaration


Rectangle : : Rectangle ( i n t l )
{
length=l ;
}
/ / defining operator overloading function outside
the cl as s d e cl a r a t i o n Rectangle operator
++( R e c t a n g l e r e c t , i n t dummy)
{
R e c t a n g l e temp =∗ t h i s ;
( r e c t . len gth )++;
r e t u r n temp ;
12
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 8

8.1. Overloading stream insertion (<<) and extraction (>>) operators


Stream insertion(<<) and extraction (>>) operators are overloaded using non-member
functions.

Overloading the stream insertion (<<) operators


Suppose our insertion(<<) operator is being overloaded for Rectangle class.
Consider the following statements:
Rectangle rect; //line 1
cin>>rect; //line 2
cout<<rect; //line 3
Line 2 will be interpreted as follows by the compiler as:
operation>>(cin, rect);
This statement is a call to operator>>() function and the function is passed two
arguments: cin and rect.
Line 3 will be interpreted as follows by the compiler as:
operator<<(cout, rect);
This is a call to operator<<() function and the function is passed two arguments:
cout and rect.
13
Note
1. cin and cout are objects of type istream and ostream respective.
2. operators<<() and operator>>() functions return objects of type ostream and
istream respectively.
Below is an example of a program, that demonstrates how insertion (<<) and ex-
traction (>>) operators are overloaded

/ ∗ −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
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

EXERCISE 29. Define “friend function”

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

EXERCISE 30. List any FOUR characteristics of a friend function


EXERCISE 31. Explain the use of “this” keyword
15
EXERCISE 32. Consider the following:
Rectangle rect(4,6); //line 1
cout<<rect; //line 2
show how the compiler will interprete line 2

16
References and Additional Reading Materials
• David Flanagan, 2005, Java in a Nutshell (5th Edition), O’Reilly Press,

• Matt Weisfeld, 2009, The Object-Oriented Thought Process, 3rd


Edition, , Addison-Wesley,

• Malik, D. S. 2002, C++ Programming: From Problem Analysis to


Program Design, Course Technology, Canada

• Pappas, H. Chris and Murray, William, H. 1996, C/C++


Programmer’s Guide, BPB Publications, New Delhi

• Flanagan, D. (2005). Java in a nutshell : a desktop quick reference.


O’Reilly (5th ed.).

• Flanagan, D. (2004). Java examples in a nutshell : a tutorial


companion to Java in a nutshell. O’Reilly (3rd ed.)

17

You might also like