Operator Function
Session No.: 36
Course Name: OOPS with C++
Course Code: E1UA203C
Instructor Name: Dr. Krishan Veer Singh
Galgotias University 1
At the end of the session
Students will be able to
Learning Outcome 1:
Outcome
Explain the1:concept of operator functions in C++ and differentiate
Explain
betweenthe needand
member of OOP.
friend functions used for operator
overloading.
Outcome 2:
Learning
Apply the Outcome
core OOP2: concepts such as Classes and
Implement operator functions to overload arithmetic, relational,
Objects, Encapsulation, Data Hiding in
and assignment operators in C++ programs.
C++
Galgotias Student-Centered Active Learning
2
Ecosystem
Revision
Galgotias University 3
1 Operator Function in C++
2 Member Function for Operator
Overloading
Session 3 Friend Function for Operator
Overloading
Outline 4 Member Function VS Friend
Function
5 Operator Overloading: Arithmetic,
Relational and Assignment
Operators
Galgotias University 4
Operator Function in C++
Operator Function:
• In C++, an operator function is a special function that allows you to overload a built-in operator
(like +, -, ==, etc.) so that it works with user-defined types (like classes and objects).
• An operator function lets you redefine how operators behave when applied to objects.
Syntax:
ReturnType operator<symbol>(parameters) { ... }
An operator function defines how an operator behaves for a class. It can be implemented
as:
• A member function (defined inside the class)
• A friend function (defined outside the class but has access to private members)
Galgotias University 5
Operator Function in C++
Example:
Let's say you have a class Complex to represent complex numbers: An operator function lets
you redefine how operators behave when applied to objects.
Here:
operator+ is the operator
function
It defines how + works for
Complex objects
Galgotias University 6
Operator Function in C++
Why Use Operator Functions?
An operator function is a user-defined function that overloads a built-in
operator so it can be used with class objects just like primitive types.
Galgotias University 7
Member Functions for Operator
Overloading
Key Properties
Overloading + for a
• Defined inside the class Vector Class
• The left operand must be an object of the
class
• Implicitly accesses the object via this
How does it work?
• v1 + v2 calls v1.operator+(v2)
Limitation
• Cannot handle cases where the left operand is
not a class object (e.g., 5 + obj)
Galgotias University 8
Friend Functions for Operator Overloading
Key Properties
•Defined outside the class but declared as friend inside
•No implicit this (all operands are passed explicitly)
Needed when:
• The left operand is not a class object (e.g., cout << obj)
• You want symmetric behavior (e.g., 5 + obj and obj + 5)
Galgotias University 9
Friend Functions for
Operator Overloading
Overloading << for Output
Why friend?
• operator<< needs access to private
members (x, y)
• The left operand is cout (not Vector), so it
can't be a member function
Galgotias University 10
Member vs Friend
Functions: Key Differences
Feature Member Function Friend Function
Outside the class (declared as
Definition Inside the class
friend)
Can be non-class (e.g., cout <<
Left Operand Must be a class object
obj)
Access to Private Direct (via this) Requires friend declaration
Asymmetric operators (e.g., obj
Use Case Symmetric operators (e.g.,5 + obj)
+ 5)
Galgotias University 11
Learning Activity 1
Galgotias University 12
Concept and
Definition
Member Function
and
Friend Function
GSCALE full form and date 13
Arithmetic Operator Overloading
Example: Overloading + and - for a Vector Class
Key Points
Return Type: Usually returns a new object (e.g.,
Vector).
Parameters: Often takes a const reference (const
Vector&).
Member vs Friend: Arithmetic operators are
typically member functions (unless symmetric
behavior is needed, e.g., 5 + obj).
Galgotias University 14
Relational Operator Overloading
Example: Overloading == and < for a Student Class
Key Points
• Return Type: Always returns a bool
(true/false).
• Common Operators: ==, !=, <, >,
<=, >=.
• Use Case: Sorting, searching, or
comparing objects.
Galgotias University 15
Assignment Operator Overloading
Example: Overloading = and +=
for a String Class
Galgotias University 16
Combined Example
Combined Example:
Complex Class
Galgotias University 17
• Arithmetic Operators: Return a new object (don’t modify operands).
• Relational Operators: Keep comparisons logical and intuitive.
• Assignment Operators:
•Always check for self-assignment.
•Return *this for chaining.
• Use const: For parameters and member functions where applicable.
Galgotias University 18
Concept and
Definition for
Operator
Overloading
Galgotias University 19
At the end of this session students will be able to
Learning Outcome 1:
Explain the concept of operator functions in C++ and
differentiate between member and friend functions used
for operator overloading.
Learning Outcome 2:
Implement operator functions to overload
arithmetic, relational, and assignment operators
in C++ programs.
Galgotias University 20
Learning Activity 2
Galgotias University 21
Next Session Topic
Member and Non-Member Operator
Function
Galgotias University 22
Review and Reflection
of the students
Galgotias University 23