Overloading Post Increment Operator (++)

Written by Raza on. Posted in C++

Last time we saw how to overload pre increment operator in our Fraction class as a member function. Post increment operator and pre increment operator are identical in their syntax. Both are written as (++) and used in exactly the same way except to their position before and after the variable. So how to overload the post increment operator in your class? I will show you how to do this in this article. 

 

Function of Post Increment Operator:

The difference between both the increment operators has been explained to quite an extent in the previous post. For revision look at that example again.

</p>
<p>int num = 5 ;</p>
<p>int b = num++ ;</p>
<p>

 

The value of ‘num’ is stored in ‘b’ and after that post increment operator adds one to it. In short, the function which we overload for it should store the existing value of the variable, increment the original value by one and return temporary variable which is now the previous value of that variable.

Now comes the most important question. What is the difference between the functions for both pre and post increment operators? Well the answer is a simple No. There cannot be any difference at all. But there should be a way to tell the compiler which function to call. Clearly compiler cannot distinguish between the function call for these increment operators as they are pretty much identical. For this purpose, C++ solved our problem by setting up a rule. The standard is that the function for post increment operator shall have an extra parameter. That extra parameter is just a dummy parameter of integer type. It has no function at all and is not a kind of default parameter. Rather it is meant just for the compiler to choose which function to invoke. See the source code for the function and you shall get clear on everything.

 

</p>
<p>Fraction operator ++ (int)<br />
{<br />
Fraction temp = (*this) ;<br />
++(*this) ;<br />
return temp ;<br />
}</p>
<p>&nbsp;</p>
<p>int main ()<br />
{<br />
Fraction f1 (3, 2) ;<br />
cout &lt;&lt; f1++ &lt;&lt; endl ;<br />
cout &lt;&lt; f1 &lt;&lt; endl ;</p>
<p>}</p>
<p>&nbsp;</p>
<p>

Observe that in the function, we do ‘++(*this)’. Reason is that we have already overloaded the pre increment operator so there is no need to manually add one to the fraction again here. Remember, re usability of code is the thing for which we are working all this stuff. Keep in mind that overloaded function for post increment operator should return the object only by value and not by reference. The reason is obvious; you are returning the value of a local variable so you cannot return by reference. Read this post for more on when to return by value and reference. In main, the first output statement prints 3/2 and the second prints 5/2.

Finally, perhaps you remember that i advised you not to use post increment operator as it uses extra space in memory. The reason is evident now. Whenever the overloaded function for post increment is called, a temporary object is created in memory which becomes a burden on the system. So it is best not to use the post increment operator much in your program.

Tags: , , ,