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.

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)
```

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.

 `// C++ program to demonstrate ` `// prefix increment operator overloading ` ` `  `#include ` `using` `namespace` `std; ` ` `  `class` `Integer { ` `private``: ` `    ``int` `i; ` ` `  `public``: ` `    ``// Parameterised constructor ` `    ``Integer(``int` `i = 0) ` `    ``{ ` `        ``this``->i = i; ` `    ``} ` ` `  `    ``// Overloading the prefix operator ` `    ``Integer operator++() ` `    ``{ ` `        ``Integer temp; ` `        ``temp.i = ++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 pre-increment operator ` `    ``Integer i2 = ++i1; ` ` `  `    ``cout << ``"After pre increment: "``; ` `    ``i2.display(); ` `} `

Output:

```Before increment: i = 3
After pre increment: i = 4
```

 `// C++ program to demonstrate ` `// postfix increment operator overloading ` ` `  `#include ` `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``) ` `    ``{ ` `        ``Integer temp; ` `        ``temp.i = 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: "``; ` `    ``i2.display(); ` `} `

Output:

```Before increment: i = 3
After post increment: i = 3
```

Similarly we can also overload the decrement operator as follows

 `// C++ program to demonstrate ` `// prefix decrement operator overloading ` ` `  `#include ` `using` `namespace` `std; ` ` `  `class` `Integer { ` `private``: ` `    ``int` `i; ` ` `  `public``: ` `    ``// Parameterised constructor ` `    ``Integer(``int` `i = 0) ` `    ``{ ` `        ``this``->i = i; ` `    ``} ` ` `  `    ``// Overloading the prefix operator ` `    ``Integer operator--() ` `    ``{ ` `        ``Integer temp; ` `        ``temp.i = --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 pre-decrement operator ` `    ``Integer i2 = --i1; ` ` `  `    ``cout << ``"After pre decrement: "``; ` `    ``i2.display(); ` `} `

Output:

```Before decrement: i = 3
After pre decrement: i = 2
```

 `// C++ program to demonstrate ` `// postfix decrement operator overloading ` ` `  `#include ` `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``) ` `    ``{ ` `        ``Integer temp; ` `        ``temp.i = 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: "``; ` `    ``i2.display(); ` `} `

Output:

```Before decrement: i = 3
After post decrement: i = 3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.