Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why the data type int is passed as a parameter in the operator function? is it compulsory to pass this in case of post increment ++?
C++
#include<iostream.h>
#include<conio.h>

class x
{
      private:
              int a;
      public:
             x(int j)
             {
                   a=j;
             }
             x operator++(int)        //postfix 
             {
                    return(a++);
             }
             void display()
             {
                  cout<<a;
             }
};

int main()
{
    x o1(10);
    cout<<"Before increment :";
    o1.display();
    o1++;
    cout<<endl;
    cout<<"After increment :";
    o1.display();
    getch();
    return 0;
}
Posted
Updated 11-Aug-11 11:47am
v4

1 solution

Simple: to differentiate Pre-increment (no type) from Post-increment (with type)

Otherwise you would have to have two different names, with is a bit against the principles of OOP. This way it just looks like a standard overloaded operator.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900