Chapter 5
Operator Overloading
in c++
Introduction
• Operator overloading is a feature in C++ that allows you to redefine the behavior of
operators when they are used with user-defined types.
• It enables you to give special meaning to operators when they are applied to objects of
your own classes.
• In C++, operators are typically associated with built-in types, such as integers, floating-
point numbers, etc.
• However, with operator overloading, you can extend the functionality of these operators
to work with objects of user-defined classes.
• For example, you can define what it means to add two objects of a class together (+
operator), compare two objects for equality (== operator), or perform other operations
by overloading the respective operators within your class definition.
• Operator overloading allows you to write code that is more intuitive and expressive,
making it easier to work with user-defined types in a manner similar to built-in types.
• However, it should be used judiciously to maintain code readability and avoid confusion.
Introduction
• In C++, we can define how operators behave for user-defined types
like class and structures.
• By overloading the operators, we can give additional meaning to the
operators like +-*/=.,= etc., which by default are supposed to work only on
standard data types like int, float, char, void etc.
• It’s a type of polymorphism in which an operator is overloaded to give it the
user-defined meaning.
• For example, The + operator, when used with values of type int, returns their
sum.
• However, when used with objects of a user-defined type, it is an error.
• In this case, we can define the behavior of the + operator to work with objects
as well.
• This concept of defining operators to work with objects and structure variables
is known as operator overloading.
Syntax for C++ Operator Overloading
• The syntax for overloading an operator is similar to that of function with the addition of
the operator keyword followed by the operator symbol.
Here,
• returnType - the return type of the function.
• operator - a special keyword
• symbol - the operator we want to overload (+, <, -, ++, etc).
• arguments - the arguments passed to the function
• C++ provides a special function to change the current functionality of some operators
within its class which is often called as operator overloading.
• Operator Overloading is the method by which we can change the function of some
specific operators to do some different tasks.
Why is operator overloading important
in C++?
• Operator overloading is important in C++ for several
reasons:
[Link] and Readability: Allows you to write code that closely resembles mathematical
expressions and natural language constructs.
This makes the code more expressive and easier to understand, enhancing readability and maintainability.
[Link] with Built-in Types: By overloading operators for user-defined types, you can achieve
consistency with the behavior of built-in types in C++.
This allows users to work with user-defined types in a manner similar to built-in types, reducing the learning curve
and making the code more intuitive.
[Link] Reusability: Promotes code reusability by allowing you to define custom behaviors for operators
that can be reused across multiple contexts and projects.
Once overloaded, operators can be used with different types of objects without the need for redundant
implementation.
[Link] and Flexibility: Gives you the flexibility to define custom behaviors for operators
based on the specific requirements of your classes.
This customization allows you to tailor the behavior of operators to match the semantics of your classes, leading to
more flexible and powerful abstractions.
[Link] with Standard Library: Many classes in the C++ Standard Library overload operators to
provide convenient and efficient operations on standard containers, iterators, and other data
structures.
By understanding operator overloading, you can leverage these features and seamlessly integrate your own classes
with the standard library.
Types of Operator Overloading in C++
• There are two types of operator overloading:
o Function overloading.
o Operator overloading.
Operator Overloading
• Operator overloading is a compile-time polymorphism.
• It is an idea of giving special meaning to an existing operator in C++ without changing its original
meaning.
• 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 +.
o Other example classes where arithmetic operators may be overloaded are Complex Numbers, Fractional
Numbers, Big integers, etc.
• Here, variables “a” and “b” are of types “int” and “float”, which are built-in data types.
• Hence the addition operator ‘+’ can easily add the contents of “a” and “b”.
• This is because the addition operator “+” is predefined to add variables of built-in data
type only.
• Operator Overloading can be done by using two approaches, i.e.
o Unary operators
o Binary operators
o Special operators ( [ ], (), etc)
In this example, we have 3 variables “a1”, “a2” and “a3” of type
“class A”.
Here we are trying to add two objects “a1” and “a2”, which are of
user-defined type i.e. of type “class A” using the “+” operator.
This is not allowed, because the addition operator “+” is predefined to
operate only on built-in data types.
But here, “class A” is a user-defined type, so the compiler generates
an error.
This is where the concept of “Operator overloading” comes in.
Now, if the user wants to make the operator “+” add two class
objects, the user has to redefine the meaning of the “+” operator
such that it adds two class objects.
This is done by using the concept of “Operator overloading”.
So the main idea behind “Operator overloading” is to use C++
operators with class variables or class objects.
Redefining the meaning of operators really does not change their
original meaning; instead, they have been given additional
meaning along with their existing ones.
Criteria/Rules to Define the Operator Function
1. The operator function must be either a non-static (member function), global
free function or a friend function., the binary operator should have only one
argument and the unary should not have an argument.
[Link] the case of a friend function, the binary operator should have only two
arguments and the unary should have only one argument.
[Link] that cannot be overloaded are .* :: ?:
[Link] that cannot be overloaded when declaring that function as friend
function are = () [] ->.
[Link] operators can only be overloaded, but the new operators cannot be
overloaded.
[Link] overloaded operator contains at least one operand of the user-defined data type.
[Link] cannot use friend function to overload certain operators. However, the member
function can be used to overload those operators.
[Link] unary operators are overloaded through a member function take no explicit
arguments, but, if they are overloaded by a friend function, takes one argument.
[Link] binary operators are overloaded through a member function takes one explicit
argument, and if they are overloaded through a friend function takes two explicit
Function Overloading in C++
• C++ allows you to specify more than one definition for
a function name or an operator in the same scope,
which is called function overloading and operator
overloading respectively.
• When you call an overloaded function or operator, the
compiler determines the most appropriate definition to
use, by comparing the argument types you have used to
call the function or operator with the parameter types
specified in the definitions.
• The process of selecting the most appropriate overloaded
function or operator is called overload resolution.
Function overloading
•Function overloading is a feature of
C++ that allows multiple functions
with the same name but different
parameter lists (number, type, or
order of parameters).
•Function overloading is the process
of defining more than one function
with the same name but different
parameters.
Function Overloading continued…
• You can have multiple definitions for the same function
name in the same scope.
• The definition of the function must differ from each
other by the types and/or the number of arguments in
the argument list.
• You cannot overload function declarations that differ
only by return type.
• Following is the example where same function print() is
being used to print different data types −
Overloadable/Non-
overloadableOperators
•Scope operator (::)
•Sizeof
•typeid
•member selector(.)
•member pointer selector(*)
•ternary operator(?:)
demonstrating operator overloading within a class:
Assignment (Group)
• Create a C++ program that implements operator overloading for
a user-defined class.
• Use object-oriented programming principles to define the class
and its member functions.
• Overload the following operators by writing C++ program.
o Unary operators overloadung
o Binary Operators Overloading
o Relational Operators overloading
o Input/Output Operators overlading
o
Due Date: Dec 30,2025
++ and – Operator overloading
o Assignment Operators overloading
o Function Call() Operator Overloading
o Subscripting [] Operator Overloading
o Class Member access Operator->Overloading
• Test your implementation by creating objects of the class and
performing operations using overloaded operators.
• Document your code thoroughly and provide comments where