Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

‘Postfix ++’ And ‘Prefix ++’ Operator Overloading


1. Introduction

We know that a binary operator takes two operands in which it performs the operation. Say, for example, the addition operator adds two numbers. We call these two numbers as operands and the ‘+’ as a binary operator. Now let us deal with the number ‘-12’. Here the minus sign states the negation of the number. The negation operator operates on only one operand. So we can say the ‘-‘ as an Unary Operator. In this example, we will see how to overload the ++ Unary Operator in both prefix and postfix forms.

2. The Number Class for Prefix Overload and PostFix Overload

The class TheNumber shown below has two private integer members. The constructor with two integer parameter sets default values to the class private members. Print method of this class prints the private member values in the console output.

In the code below, we also overloaded the binary operator +. For more information on Binary operator overloading, read the article here: Binary Operator Overloading. The initial code for the class is below:

3. Overload Prefix increment Operator ++

3.1 Prefix Overload Implementation

We can code the prefix operator the same way as we did for the binary + operator. Note that the implementation does not take any argument and the calling object itself serves the data for the first operand. Have a look at the below implementation:

In this implementation, we raise both the private members by adding one to it. After this, we return the current object to the caller. Remember that the current object is the only possible operand which called this function. With this overloaded operator, now it is possible to use the prefix notation of increment on an object. For example, if the instance is K, then we can use ++K, to increment both of its private members.

3.2 Overloaded Prefix Usage

Now have a look at the usage code shown below:

In the above case, we created two objects. They are num1 and num2. The usage02 code snippet applies the prefix operator on both the objects num1 and num2 before adding them together using operator +.

3.3 Calling Sequence

The calling sequence of Prefix Overload goes like this:

  1. First, C++ calls the prefix increment for num1
  2. Secondly, the prefix increment operator for the num2 object is called.
  3. Thirdly, the binary operator + is called on both the objects num1 and num2

At step 1, the private members for the num1 object are incremented, and the object is returned. At step 2, the private members for the num2 object are incremented and num2 is returned. In the third step, we add the incremented objects using overloaded binary operator plus. So, we will see the result as shown below:

Prefix Operator Program Output

Prefix Operator Program Output

The below picture explains the above process:

Overloaded Prefix Operator Sequence

Overloaded Prefix Operator Sequence.

4. Overloading Postfix ++ Operator

4.1 Postfix Overload Implementation

In C++, we should overload the postfix operator somewhat differently. This is to help the run-time to avoid ambiguity. First, the operator function takes a dummy integer argument to differentiate this from the prefix overloaded operator. Next, in the implementation we clone the object. Then do increment on the calling object i.e. at the operand one. Lastly, return the cloned object. This way, we see the increment taking place after the statement execution. Have a look at the below implementation:

Note that we do the clone first. Then we do the increment on the local object. Finally, we return the cloned copy to the caller. So in caller perspective, the expression which contains the operator, gets only the cloned copy. After the ‘expression statement’ is done, we see the increment taking part in the operand. The parameter dummy is not doing anything here, right? This parameter is useful for the compiler to distinguish between prefix and postfix overload.

4.2 Overloaded Postfix Usage

Look at the below code that uses the postfix operator:

Here, postfix operator for num1 is called first. Then postfix operator for the num2 is called. But the postfix operator on both the cases returned a cloned copy. These copies are then added together to get a result in the object num4.

The overloaded operator returns the cloned copy. The binary plus operator acts on these clones. But, in the actual memory of Num1 and Num2, our operator function raised the internal members by 1. So, when we move to the next statement, we see the changed internal members.

Postfix Operator Output

Postfix Operator Output

4.3 Calling Sequence

For more explanation, have a look at the below picture. In the postfix operator, we make the changes on the real objects. But, we return the cloned copy which was taken before the change was applied. The binary plus operator actually operates on the cloned copies and hence we get the members with a value of two in the result object num4.

Overloaded Postfix Operator Sequence

Overloaded Postfix Operator Sequence

5. Complete code Example


Categories: C++

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.