Operator
Overloading &
Friend
Function Operator Overloading & Friend Function
Dr. Munesh
Singh
Dr. Munesh Singh
Indian Institute of Information Technology
Design and Manufacturing,
Kancheepuram
Chennai-600127
January 18, 2019
Operator Overloading
Operator
Overloading &
Friend
Function
Dr. Munesh
Singh When an operator is overloaded with multiple jobs, it is
known as operator overloading
It is a way to implement compiler time polymorphism.
Rules to Remember
Any symbol can be used as function name
If it is a valid operator in C language
If it is preceded by operator keyword
You can not overload sizeof and ?: operator
Operator Overloading
Operator
Overloading &
Friend
Binary Operator Overloading +
Function
Dr. Munesh
Singh
Operator Overloading
Operator
Overloading & Uniary Operator Overloading -
Friend
Function
Dr. Munesh
Singh
Operator Overloading
Operator
Overloading & Uniary Operator Overloading ++
Friend
Function
Dr. Munesh
Singh
Friend Function
Operator
Overloading &
Friend
Function
Dr. Munesh Friend Function is not a member function of a class to
Singh
which it is a friend
Friend function is declared in the class with friend keyword
It must be defined outside the class to which it is friend
Friend function can access any member of the class to
which it is friend
Friend function cannot access members of the class
directly
It has no caller object
It should not be defined with membership label
Friend Function Example
Operator
Overloading &
Friend
Function
Dr. Munesh
Singh
Friend Function Feature
Operator
Overloading & Friend Function can become friend to more than one class
Friend
Function
Dr. Munesh
Singh
Friend Function Feature
Operator
Overloading &
Friend
Function
Friend function overloading of Binary Operator
Dr. Munesh
Singh Overloading of operators as a friend function
Friend Function Feature
Operator
Overloading &
Friend
Function
Friend function overloading of Uniary Operator
Dr. Munesh Overloading of operators as a friend function
Singh
Friend Function
Operator
Overloading & Member function of one class can become friend to
Friend
Function another class
Dr. Munesh
Singh