S S G M C E, Shegaon
Department of Electronics and Telecommunication
Object Oriented Programming
Using C++
For
nd
2 Year EXTC
Dr. Santosh B. PATIL
sbpatil@ssgmce.ac.in +91-7020799970
Operator Overloading
Contents
• Overloading Concept
• Operator overloading
• Function overloading- already covered in Unit-II
• Type Conversion
• Built-in type to Class
• Class to Built-in type
• One class to another
Overloading Concept
•Overloading is one of the many existing features of C++ language.
•Overloading means giving additional work (meaning ) to existing entity
(Operators/functions)
•It is important techniques that has enhanced the power of extensibility of C++.
Types of overloading in C++
•Function overloading
•Operator overloading
Operator overloading-feature in C++ programming allows programmer to redefine the meaning
of an operator.
1 2 3
Operator Overloading
In C++, we can make operators to work for user defined classes. This means C++
has the ability to provide the operators with a special meaning for a data type,
this ability is known as operator overloading.
For example, we can overload an operator ‘+’ in a class like String so that we can
concatenate two strings by just using + operator.
Operator Function Syntax
What is the difference between operator functions and normal functions?
Operator functions are same as normal functions. The only differences are, name of
an operator function is always operator keyword followed by symbol of operator and
operator functions are called when the corresponding operator is used.
Implementation
Operator overloading can be done by implementing a function which can be :
•Member Function
•Non-Member Function
•Friend Function
Operator overloading function can be a member function if the Left operand is an
Object of that class, but if the Left operand is different, then Operator overloading
function must be a non-member function.
Operator overloading function can be made friend function if it needs access to the
private and protected members of class.
Operator overloading process
1.Create the class that defines the data type that is to be used in the overloading
operation.
2.Declare the operator function in the public part of the class. It may be either
member function or friend function.
3.Define the operator function to implement the required operations.
Can we overload all operators?
Almost all operators can be overloaded except few. Following is the list of operators
that cannot be overloaded.
Following operators can not be overloaded by friend function however, we can use
member function to overload these operators.
= Assignment operator
() function call operator
[] subscripting operator
-> class member access operator
Rules for Operator Overloading
1.Only the existing operators can be overloaded and new operators cannot be
overloaded
2.The overloaded operator must contain at least one operand of the user-defined
data type.
3.We do not use a friend function to overload certain operators. However, the
member functions can be used to overload those operators.
4.When unary operators are overloaded through a member function they take no
explicit arguments, but, if they are overloaded by a friend function they take one
argument.
5.When binary operators are overloaded through a member function they take
one explicit argument, and if they are overloaded through a friend function they
take two explicit arguments.
Example-1
Example-2
Example-3
Example-4- Program to show unary – operator overloading
Result-
Example-5
Example-6
Example-7
Example-8 friend function
Example-9 friend function unary
Type Conversion
• Built-in type to Class
• Class to Built-in type
• One class to another
Type Conversion
The type conversion can be performed by following ways:
Using constructor
Using Operator Overloading
Type Casting
int m1,m2,m3, sum;
flot avg;
Total=m1+m2+m3;
avg= float (total)/3
Basic to Class Type Conversion
Class Type to Basic Conversion
Class to Class Conversion