Subscribe Us

Responsive Advertisement

Advertisement

Overloading Increment and Decrement Operators in Postfix form OOP

 

Overloading Increment and Decrement Operators in Postfix form OOP




#include<bits/stdc++.h>

using namespace std;

class pre

{

    int val;

public :

       pre()

       {

           val=0;

       }

       pre(int v)

       {

           val=v;


       }

       void display()

       {


           cout<<val<<endl;

       }

       pre operator++(int)

       {

           pre duplicate(*this);

           val=val+1;

           return duplicate;

       }

       friend pre operator--(pre &,int);


};

pre operator--(pre &v,int)

{

    pre duplicate(v);

    v.val-=1;

    return duplicate;

}

int main()

{

  pre a(10);

   a.display();

   (a++).display();

  a++;

  a.display();

  a--;

   a.display();



    return 0;

}


Post a Comment

0 Comments