1
Contents
Operator Overloading ................................................................................................ 2
Overloading + Operator .......................................................................................... 3
1. Overloading Unary Operator ................................................................................ 4
2. Overloading Binary Operator ............................................................................... 5
Operator Overloading With Return Values ..................................................................... 7
1.Return by Value .................................................................................................. 7
2.Return by Reference ............................................................................................ 8
UoH Object Oriented Programming BSDS
2
Operator Overloading
▪ The process of defining additional meanings of operators is known as
operator overloading.
▪ It enables an operator to perform different operations depending on the type
of operands.
▪ It also enables the operators to process the user-defined data types.
▪ The basic arithmetic operators such as +, -, *, / normally work with basic
types such as int, float and long etc.
▪ The application of these operators with basic types are already defined in
the language. However, an error will occur if these operators are used with
user-defined objects.
Example
The addition operator is used to add two numeric values. Suppose a, b and c are
three integer variables. The following statement will add the contents of a and b
and store the result in variable c:
c = a + b;
The addition operator already knows how to process integer operands. But it does
not know how to process two user-defined objects. The above statement will
generate error if a, b and c are objects of a class. However, operator overloading
can enable the addition operator to manipulate two objects.
Syntax
The syntax of overloading an operator is as follows:
return_type operator op ( )
{
function body;
}
return_type
It indicates the type of value returned by the member function.
UoH Object Oriented Programming BSDS
3
Operator: It is the keyword that indicates that the member function is used to overload an
operator.
Op: It is the symbol of operator to be overloaded.
Overloading + Operator
Example
Let's add two Point objects (x, y coordinates).
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
// Overload + operator to add two Points
Point operator+(const Point& other) {
return Point(x + other.x, y + other.y);
}
void display() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(2, 3), p2(4, 5);
Point p3 = p1 + p2; // Calls operator+
cout << "Result of p1 + p2 = ";
p3.display();
return 0;
UoH Object Oriented Programming BSDS
4
Operator Overloading can be done by using two approaches, i.e.
1. Overloading Unary Operator.
2. Overloading Binary Operator.
Criteria/Rules to Define the Operator Function
1. In the case of a non-static member function, the binary operator should
have only one argument and the unary should not have an argument.
2. In the case of a friend function, the binary operator should have only two
arguments and the unary should have only one argument.
3. Operators that cannot be overloaded are .* :: ?:
4. Operators that cannot be overloaded when declaring that function as
friend function are = () [] ->.
5. The operator function must be either a non-static (member function),
global free function or a friend function.
1. Overloading Unary Operator
Let us consider overloading (-) unary operator. In the unary operator function, no
arguments should be passed. It works only with one class object. It is the
overloading of an operator operating on a single operand.
Example: Assume that class Distance takes two member objects i.e. feet and inches, and
creates a function by which the Distance object should decrement the value of feet and inches
by 1 (having a single operand of Distance Type).
UoH Object Oriented Programming BSDS
5
Code
#include <iostream> int main()
using namespace std; {
class Distance { Distance d1(8, 9);
public: // Use (-) unary operator by
int feet, inch; // single operand
// Constructor to initialize the object's -d1;
value return 0;
Distance(int f, int i) }
{ Output
this->feet = f; Feet & Inches(Decrement): 7'8
this->inch = i;
}
// Overloading(-) operator to perform
decrement operation of Distance object
void operator-()
{
feet--;
inch--;
cout << "\nFeet & Inches(Decrement):
" <<
feet << "'" << inch;
}
};
UoH Object Oriented Programming BSDS
6
2. Overloading Binary Operator
In the binary operator overloading function, there should be one argument to be passed. It is
the overloading of an operator operating on two operands. Below is the C++ program to show
the overloading of the binary operator (+) using a class Distance with two distant objects.
Code:
#include <iostream> // Driver Code
using namespace std; int main()
class Distance { {
public: Distance d1(8, 9);
int feet, inch; Distance d2(10, 2);
Distance() Distance d3;
{ // Use overloaded operator
this->feet = 0; d3 = d1 + d2;
this->inch = 0; cout << "\nTotal Feet & Inches: " << d3.feet
} << "'" << d3.inch;
Distance(int f, int i) return 0;
{
this->feet = f; }
this->inch = i; Output
} Total Feet & Inches: 18'11
// Overloading (+) operator to perform
addition of two distance
// Call by reference
Distance operator+(Distance& d2)
{
// Create an object to return
Distance d3;
d3.feet = this->feet + d2.feet;
d3.inch = this->inch + d2.inch;
// Return the resulting object
return d3;
}
};
UoH Object Oriented Programming BSDS
7
Operator Overloading With Return Values
Operator overloading allows the redefinition of built-in operators for user-defined
types. The return value of an overloaded operator is crucial for determining how
the operator interacts with objects and how it can be used in expressions. The
choice of return type depends on the operator's behavior and the desired
semantics.
1.Return by Value
▪ You return a new copy of the result — not linked to the original object.
Used When:
▪ You want to return a temporary result that won't be modified later.
▪ When an operator creates a new object or value, it should typically return
by value. This ensures that the result is a distinct object, separate from the
operands. Arithmetic operators (+, -, \*, /) and operators that produce a new
object (e.g., string concatenation) usually fall into this category.
Code
class Complex {
public:
double real, imag;
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
};
In the example above, the operator+ returns a new Complex object by value, representing the
sum of two complex numbers.
UoH Object Oriented Programming BSDS
8
2.Return by Reference
You return a reference to the same object, so modifications affect the original.
Used When:
• You want to allow chaining (a += b += c)
• You want to avoid unnecessary copying, especially with large objects.
When an operator modifies an existing object, it should generally return a
reference to that object. This allows for chaining operations and modifying the
original object directly. Assignment operators (=, +=, -=, \*=, /=),
increment/decrement operators (++, --), and input/output stream operators (<<,
>>) commonly return by reference.
Example:
class Vector {
public:
double x, y;
Vector(double x = 0, double y = 0) : x(x), y(y) {}
Vector& operator+=(const Vector& other) {
x += other.x;
y += other.y;
return *this;
}
};
In this example, the operator+= modifies the Vector object and returns a reference to it,
allowing for chained assignments like v1 += v2 += v3;.
UoH Object Oriented Programming BSDS