+ | - | * | / | % | ^ |
& | | | ~ | ! | , | = |
< | > | <= | >= | ++ | - |
<< | >> | == | != | && | || |
+= | -= | /= | %= | ^= | &= |
|= | *= | <<= | >>= | [ ] | ( ) |
→ | →* | new | new[] | delete | delete[] |
#include<iostream>
using namespace std;
//Increment and Decrement overloading
class IncreDecre
{
private:
int cnt ;
public:
IncreDecre() //Default constructor
{
cnt = 0 ;
}
IncreDecre(int C) // Constructor with Argument
{
cnt = C ;
}
IncreDecre operator ++ () // Operator Function Definition for prefix
{
return IncreDecre(++cnt);
}
IncreDecre operator ++ (int) // Operator Function Definition with dummy argument for postfix
{
return IncreDecre(cnt++);
}
IncreDecre operator -- () // Operator Function Definition for prefix
{
return IncreDecre(--cnt);
}
IncreDecre operator -- (int) // Operator Function Definition with dummy argument for postfix
{
return IncreDecre(cnt--);
}
void show()
{
cout << cnt << endl ;
}
};
int main()
{
IncreDecre a, b(5), c, d, e(2), f(5);
cout<<"Unary Increment Operator : "<<endl;
cout << "Before using the operator ++()\n";
cout << "a = ";
a.show();
cout << "b = ";
b.show();
++a;
b++;
cout << "After using the operator ++()\n";
cout << "a = ";
a.show();
cout << "b = ";
b.show();
c = ++a;
d = b++;
cout << "Result prefix (on a) and postfix (on b)\n";
cout << "c = ";
c.show();
cout << "d = ";
d.show();
cout<<"\n Unary Decrement Operator : "<<endl;
cout << "Before using the operator --()\n";
cout << "e = ";
e.show();
cout << "f = ";
f.show();
--e;
f--;
cout << "After using the operator --()\n";
cout << "e = ";
e.show();
cout << "f = ";
f.show();
c = --e;
d = f--;
cout << "Result prefix (on e) and postfix (on f)\n";
cout << "c = ";
c.show();
cout << "d = ";
d.show();
return 0;
}