Limitations of Increment and Decrement Operators Assignment Help

Assignment Help: >> Functions, Arguments and Overloading - Limitations of Increment and Decrement Operators

Limitations of increment and decrement operators

The prefix notation causes a variable to be updated before its values are used in the expression; therefore the postfix notation causes it to be updated after its value is used. Though, the statement

Idx1=++idx2;

Has exactly the similar effect as the statement

Idx1=idx2++;

When ++ and - operators are overloaded, there is no distinction among the prefix and postfix overloaded operator function. That problem is circumvented in advanced implementations of   C++ that provides additional syntax to express and distinguish among prefix and postfix overloaded operator functions. A new syntax to denoted postfix operator overloaded function is:

Operator ++(int)

The program index5.cpp describes the invocation of prefix and postfix operator functions.

#include<iostream.h>

class index

{

private:

int value;

public:

index()

{

value=0;

}

int getindex()

{

return value;

}

index operator ++()

{

return index (++value);

}

index operator ++(int)

{

return index(value++);

}

};

void main()

{

Run

index idx1(2),idx2(2),idx3,idx4;

cout <<"\n Index1 = "<< idx1.getindex();

cout <<"\n Index2 = "<< idx2.getindex();

idx3=idx1++;

idx4=++idx2;

idx2++;

cout <<"\nIndex1="<<idx1.getindex();

cout <<"\nIndex2="<<idx2.getindex();

cout <<"\nIndex3="<<idx3.getindex();

cout <<"\nIndex4="<<idx4.getindex();

}

Index1=2

Index2=2

Index1=3

Index2=2

Index1=3

Index1=3

Within the postfix operator ++ (int) function, first a nameless object along with the old index value is created and after that, the index value is updated to achieve the intended operation. The compiler will only make a call to this function for postfix operation only the responsibility of achieving this test on the programmer.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd