You are on page 1of 15

OBJECT ORIENTED

PROGRAMMING
CSE 202. Winter 23-24

Lecture 11
Operator Overloading – Part One

PRANAV BISHT
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
IIT(ISM) DHANBAD

*special thanks to Prof. Saurabh Srivastava for his original slides


Revisit – Overloading
• We had discussed the concept of Overloading in the first Unit
• Overloading could be applicable to methods or operators
• In case of methods, overloading refers to the use of multiple definitions for a
function/method …
• … which each version having a different signature (i.e., argument list)
• For operators, it meant providing a meaning to an operator, which is not otherwise defined

• … e.g., providing a definition in code for the “addition of two objects using the + operator”

• Now we will have a deeper look at Operator Overloading


• Although not the best example, we will overload two operators for our Toy class
• The > operator has the meaning “costlier of the two” whereas …
• … the < operator has the meaning “cheaper of the two”
Overloaded Operators
Assume that you have a class to represent a complex number
◦ Typically, you’ll need two fields in this class – one for the real part and the other for the imaginary
part
Now, if you have two objects of this class – representing two complex numbers …
◦ … how can you add them together? On the same lines, how can you subtract one from another?

One way to do so will be by defining methods – member functions or global functions


◦ In this case, typical expressions that you will see will include
◦ Complex c3 = c1.add(c2);
◦ Complex c3 = add(c1, c2);

Wouldn’t it be better if you could simply write something like Complex c3 = c1 + c2; ?
◦ In C++, it is possible to do so by overloading the + operator “for the Complex class”

Overloading the operator means providing or changing the definition for an expression …
◦ … containing that operator “for the said type of operands”

Pranav Bisht | Dept. of CSE| IIT (ISM) Dhanbad


A word on using Operator Overloading
Keep it in mind that everything you can do with overloaded operators can also be done
otherwise
◦ For example, with an appropriately named member function
◦ In fact, in most some cases it is better to define methods than to overload operators
◦ For example compare the two expressions:
t3 = t1<t2; and
t3 = cheaper_of_the_two(t1, t2);
◦ The latter is probably better for readability of the code

Unless the semantics of the operator is really obvious - I strongly suggest against overloading
operators
◦ It may lead to code that is not readable and hard to maintain !

Pranav Bisht | Dept. of CSE| IIT (ISM) Dhanbad


My Take
• Operator overloading is a feature which gives additional power to the programmer.
• “With great power comes great responsibility”.
• The onus is on the programmer to not abuse the feature.
• For example, if I overload + to mean logical OR and * for logical AND is somewhat bad and confusing
for other programmers working on the same project.
• However, overloading + as matrix addition and * as matrix multiplication would be perfectly fine,
IMHO.
• Even if you use methods instead of operator overloading, one can misuse
• For example, foo(a,b) does not give any hint about the functionality of foo, or even nastier, someone
using multiply(a,b) while actually adding a,b inside its definition.
• While overloading, think if this particular definition of the operator will be clear enough for
other people to not misunderstand or misinterpret.
• Keep in mind that the things which are “pretty clear from the context” differ from person to person.

Pranav Bisht | Dept. of CSE| IIT (ISM) Dhanbad


Operator Overloading with Toy class (1/5)
Operator Overloading with Toy class (1/5)

Here, you can see two rather weird expressions – one that applies the > operator on toy objects, and the other
where you see the < operator doing something similar

This is because we have overloaded these operators for the Toy class

We define > operator as the “costly operator” (it returns the costlier of the two toys) and < as the “cheap operator”
(it returns the cheaper of the two toys)
Operator Overloading with Toy class (2/5)
This is the output of the code that we saw

Observe how the two operators worked to find


out the cheapest and costliest toys out of the
three

Let us see how the operations to be performed by


these operators are defined
Operator Overloading with Toy class (3/5)
There are two ways in which an operator’s overloaded
definition can be provided
Operator Overloading with Toy class (3/5)
There are two ways in which an operator’s overloaded
definition can be provided

You can provide it as a member function of the class


Operator Overloading with Toy class (3/5)
There are two ways in which an operator’s overloaded
definition can be provided

You can provide it as a member function of the class …

… or, as a non-member function that is almost always


declared a friend to this class
Member vs Friend Function forms
• Definitions for the operators are provided in the form of functions (for a binary operator)
• The name of the definition function is the keyword operator, followed by the operator
• If the method is a member function of the class, the calling object is provided implicitly to the definition
• e.g. when you write the expression (c1 + c2), it is equivalent to c1.add(c2)
• If the method is a non-member function, the function is passed two arguments
• e.g. when you write the expression (c1 + c2), it is equivalent to add(c1, c2)

• In most cases, the return type is the same as the class for which the operator is being overloaded
• This is useful, if we wish to do chaining of the operands for binary operators …
• … e.g., even though + is a binary operator, meaning that it operates over exactly two operands, …
• … it is perfectly fine to write expressions like (a + b + c), which maybe interpreted as ((a + b)
+ c)

• Usually, to avoid creation of a new object, we pass and return values by reference
Operator Overloading with Toy class (4/5)

This is the case where we provide the definition for the overloaded operator as a member function of the class
(observe the Toy:: part preceding the name of the method)

Observe here that we are only taking one argument in the function – the second argument to be precise, in the
expression t1>t2, whereas the first object invokes the method (and hence, is implicitly available)

For example, in the above case, get_price() implies t1.get_price() and other_toy.get_price()
implies t2.get_price()
Operator Overloading with Toy class (5/5)

This is the case where we provide the definition for the overloaded operator as a friend function of the class

When the definition is provided as a friend function, both objects must be caught as two distinct parameters

For the expression t1<t2, the first_toy represents t1 and second_toy represents t2
Homework !!
• The non-member function that provided definition for the < operator is a friend to the class
Toy
• While it is common to declare such functions as friends of the class, in our case, it is not necessary
• Try to code with minor modifications, where the function is not a friend to Toy

You might also like