Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

OPERATOR

OVERLOADING
Group 5
Group Members
Name Gr No Roll No
Atharva Uday Kalase 11911293 33
Manan Hude 11910742 13
Gokul Raj 11911323 04
Mahesh Gunjawate
Introduction
Operator overloading is an important branch of Polymorphism in
C++.

Polymorphism

Compile Time Runtime

Function Operator Virtual


Overloadin Overloadin Functions
g g
3
What is Operator Overloading?

•Operator overloading is a specific case of polymorphism in


which some or all operators like +, = or = = are treated as
polymorphic functions and as such have different behaviors
depending on the types of its arguments.
Operator Overloading
 Operator overloading is a specific case of polymorphism in
which some or all of operators like +, =, or == have
different implementations depending on the types of their
arguments.
 Operator overloading gives us the opportunity to redefine the
C++ language.
 C++ permits us to add two variables of user defined types
with the same syntax that is applied to the basic data types.
Operator Overloading
We can overload all c++ operators except:
A. Class member access operator(.)
B. Scope resolution operator(::)
C. Size operator(sizeof)
D. Conditional operator(?:)
Defining Operator Overloading
General syntax for operator overloading is:
return
type classname :: operator op(arglist)
{
Function body
}
• For e.g.:
• vector operator +(vector);
•vector is a data type of class.
Defining Operator Overloading
Here,
• type is the return type of the function.
• operator is a keyword.
• Symbol(op( )) is the operator we want to overload. Like: +, <, -, +
+, etc.
• arglist is the arguments passed to the function.
Operator Overloading Process
The process of overloading involves the following steps:
Create a class that defines the data type that is to be used in the
overloading operation.
Declare the operator function operator op() in the public part
of the class.
Define the operator function to implement the required
operations.
Types of operator overloading
• Operator Overloading can be done by using three approaches, they are
1. Overloading unary operator.
2. Overloading binary operator.
3. Overloading binary operator using a friend function.
• Below are some criteria/rules to define the operator function:
• In case of a non-static function, the binary operator should have only one argument and unary
should not have an argument.
• In the case of a friend function, the binary operator should have only two argument and unary
should have only one argument.
• All the class member object should be public if operator overloading is implemented.
Operator Overloading in Binary
Operators
• Binary operators work on two operands. For example,
• result = num + 9;
• Here, + is a binary operator that works on the operands num and 9.
• When we overload the binary operator for user-defined types by using the
code:
• obj3 = obj1 + obj2;

• The operator function is called using the obj1 object and obj2 is passed as an
argument to the function.
Binary Operator Overloading Example 1

Output:
Binary Operator Overloading Example 2

Output:
Overloading unary operator.

• The unary operators operate on a single operand and following are the
examples of Unary operators −
• The increment (++) and decrement (--) operators.
• The unary minus (-) operator.
• The logical not (!) operator.
• The unary operators operate on the object for which they were called and
normally, this operator appears on the left side of the object, as in !obj, -obj,
and ++obj but sometime they can be used as postfix as well like obj++ or obj--.
Unary Operator Overloading Example

Output:
Friend Function using Operator Overloading in C++
• Friend function using operator overloading offers better flexibility to
the class.
• These functions are not a members of the class and they do not have
'this' pointer.
• When you overload a unary operator you have to pass one argument.
• When you overload a binary operator you have to pass two
arguments.
• Friend function can access private members of a class directly.
Syntax:
friend return-type operator operator-symbol (Variable 1, Varibale2)
{
//Statements;
}
Example of Unary operator overloading
using Friend function
Example Continue....
Output:
Values of A, B & C
10
20
30

Before Overloading
10
20
30

After Overloading
-10
-20
-30
Advantages of Operator Overloading
• It allows you to provide an intuitive interface to users of
your class, plus makes it possible for templates to work
equally well with classes and built-in/intrinsic types.
• Operator overloading allows C/C++ operators to have
user-defined meanings on user-defined types (classes).
• It' allows the "seamless extension" of the built-
in operators.by user-defined data types.
Disadvantages of Operator Overloading
• Only built-in operators can be overloaded.
• Degree or arity of the operators cannot be changed.
• Precedence and associativity of the operator cannot be changed.
• Overloaded operator cannot have default arguments, except for ()
operator.
• At least one operand must be of user-defined type.
• =,
[], (), -> must be defined as member functions. Remaining
operators can be either member or non-member functions.
• Operators like ::, dot, and ?: cannot be overloaded.
References
• https://www.startertutorials.com/blog/operator-overloading-
c.html#Rules_for_Operator_Overloading
• https://www.quora.com/Why-is-operator-overloading-useful
• https://www.geeksforgeeks.org/types-of-operator-overloading-in-c/
• https://www.decodejava.com/cpp-operator-overloading-with-friend-
function.htm
• https://www.tutorialride.com/cpp/friend-function-using-operator-overloading-
in-c.htm

You might also like