Increment (++) and Decrement (--) Operator Overloading in C++
Last Updated :
16 Nov, 2022
Operator overloading is a feature in object-oriented programming which allows a programmer to redefine a built-in operator to work with user-defined data types.
Why Operator Overloading?
Let's say we have defined a class Integer for handling operations on integers. We can have functions add(), subtract(), multiply() and divide() for handling the respective operations. However, to make the code more intuitive and enhance readability, it is preferred to use operators that correspond to the given operations(+, -, *, / respectively) i.e. we can replace the following code.
Example:
Replace
i5 = divide(add(i1, i2), subtract(i3, i4))
by a simpler code:
i5 = (i1 + i2) / (i3 - i4)
Overloading the Increment Operator
The operator symbol for both prefix(++i) and postfix(i++) are the same. Hence, we need two different function definitions to distinguish between them. This is achieved by passing a dummy int parameter in the postfix version.
Here is the code to demonstrate the same.
Example: Pre-increment overloading
CPP
// C++ program to demonstrate
// prefix increment operator overloading
#include <bits/stdc++.h>
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the prefix operator
Integer& operator++()
{
++i;
// returned value should be a reference to *this
return *this;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before increment: ";
i1.display();
// Using the pre-increment operator
Integer i2 = ++i1;
cout << "After pre increment: " << endl;
cout << "i1: ";
i1.display();
cout << "i2: ";
i2.display();
}
OutputBefore increment: i = 3
After post decrement:
i1: i = 4
i2: i = 4
Example: Post-Increment Overloading
CPP
// C++ program to demonstrate
// postfix increment operator
// overloading
#include <bits/stdc++.h>
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator++(int)
{
// returned value should be a copy of the object before increment
Integer temp = *this;
++i;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before increment: ";
i1.display();
// Using the post-increment operator
Integer i2 = i1++;
cout << "After post increment: " << endl;
cout << "i1: ";
i1.display();
cout << "i2: ";
i2.display();
}
OutputBefore increment: i = 3
After post increment:
i1: i = 4
i2: i = 3
Overloading the Decrement Operator
Similarly, we can also overload the decrement operator as follows:
Example: Pre-Decrement Overloading
CPP
// C++ program to demonstrate
// prefix decrement operator
// overloading
#include <bits/stdc++.h>
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the prefix operator
Integer& operator--()
{
--i;
// returned value should be a reference to *this
return *this;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before decrement: ";
i1.display();
// Using the pre-decrement operator
Integer i2 = --i1;
cout << "After pre decrement: " << endl;
cout << "i1: ";
i1.display();
cout << "i2: ";
i2.display();
}
OutputBefore decrement: i = 3
After pre decrement:
i1: i = 2
i2: i = 2
Example: Post-Decrement Overloading
CPP
// C++ program to demonstrate
// postfix decrement operator
// overloading
#include <bits/stdc++.h>
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator--(int)
{
// returned value should be a copy of the object before decrement
Integer temp = *this;
--i;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before decrement: ";
i1.display();
// Using the post-decrement operator
Integer i2 = i1--;
cout << "After post decrement: " << endl;
cout << "i1: ";
i1.display();
cout << "i2: ";
i2.display();
}
OutputBefore decrement: i = 3
After post decrement:
i1: i = 2
i2: i = 3
Similar Reads
Overloading New and Delete operator in c++
The new and delete operators can also be overloaded like other operators in C++. New and Delete operators can be overloaded globally or they can be overloaded for specific classes. If these operators are overloaded using member function for a class, it means that these operators are overloaded only
5 min read
C++ Increment and Decrement Operators
Prerequisite: Operators in C++ What is a C++ increment Operator? The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only. There a
4 min read
Different Ways of Operator Overloading in C++
In C++, operator overloading is the concept that allows us to redefine the behavior of the already existing operator for our class. C++ provides a special function called operator function that can be used to achieve operator overloading. In this article, we will learn the different ways in which we
6 min read
C++ Assignment Operator Overloading
Prerequisite: Operator OverloadingThe assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types.Assignment operator overloading is binary operator overloading.Overloading ass
4 min read
Operator Overloading in C++
in C++, 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.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
Increment (Decrement) operators require L-value Expression
What will be the output of the following program? c #include<stdio.h> int main() { int i = 10; printf("%d", ++(-i)); return 0; } A) 11 B) 10 C) -9 D) None Answer: D, None - Compilation Error. Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) opera
1 min read
Overloading stream insertion (<>) operators in C++
In C++, stream insertion operator "<<" is used for output and extraction operator ">>" is used for input. We must know the following things before we start overloading these operators. 1) cout is an object of ostream class and cin is an object of istream class 2) These operators must be
2 min read
Input/Output Operators Overloading in C++
Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(>>/<<) Overloading in C++ We c
2 min read
Pre-increment and Post-increment in C/C++
In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning
3 min read
Pre-increment (or pre-decrement) With Reference to L-value in C++
Prerequisite: Pre-increment and post-increment in C/C++ In C++, pre-increment (or pre-decrement) can be used as l-value, but post-increment (or post-decrement) can not be used as l-value. For example, following program prints a = 20 (++a is used as l-value) l-value is simply nothing but the memory l
2 min read