decrement & increment
//operator++ and operator--
#include<iostream>
using namespace std;
class decInc
{
public:
decInc(int m=5):k(m) { };
decInc& operator++();
decInc operator++(int);
decInc& operator--();
decInc operator--(int);
int getK()
{
return k;
}
private:
int k;
};
decInc& decInc::operator++() //++K
{
k=k+1;
return *this;
}
decInc decInc::operator++(int) //K++
{
decInc temp=*this;
k=k+1;
return temp;
}
decInc& decInc::operator--() //--K
{
k=k-1;
return *this;
}
decInc decInc::operator--(int) //K--
{
decInc temp=*this;
k=k-1;
return temp;
}
int main()
{
int x;
decInc d;
++d; // 6
d++; // still 6;
x=d.getK(); // x=7 now ;
cout<<x<<endl;
--d; // 6
d--; // 6
x=d.getK(); //x=5 now ;
cout<<x<<endl;
return 0;
}