Welcome to the World of Online Learning:
Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Write a C++ program for Increment ++ and Decrement — Operator Overloading
PROGRAM: C++ program for Increment ++ and Decrement — Operator Overloading
/* C++ program for Increment ++ and Decrement -- Operator Overloading */
1: Prefix ++ Increment Operator Overloading with no return type
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
void operator ++()
{ ++i; }
void Display()
{ cout << "i=" << i << endl; }
};
int main()
{
Check obj;
// Displays the value of data member i for object obj
obj.Display();
// Invokes operator function void operator ++( )
++obj;
// Displays the value of data member i for object obj
obj.Display();
return 0;
}
2: Prefix Increment ++ operator overloading with return type
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
// Return type is Check
Check operator ++()
{
Check temp;
++i;
temp.i = i;
return temp;
}
void Display()
{ cout << "i = " << i << endl; }
};
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
obj1 = ++obj;
obj.Display();
obj1.Display();
return 0;
}
3: Postfix Increment ++ Operator Overloading
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
Check operator ++ ()
{
Check temp;
temp.i = ++i;
return temp;
}
// Notice int inside barcket which indicates postfix increment.
Check operator ++ (int)
{
Check temp;
temp.i = i++;
return temp;
}
void Display()
{ cout << "i = "<< i <<endl; }
};
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
// Operator function is called, only then value of obj is assigned to obj1
obj1 = ++obj;
obj.Display();
obj1.Display();
// Assigns value of obj to obj1, only then operator function is called.
obj1 = obj++;
obj.Display();
obj1.Display();
return 0;
} 4: Operator Overloading of Decrement -- Operator
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(3) { }
Check operator -- ()
{
Check temp;
temp.i = --i;
return temp;
}
// Notice int inside barcket which indicates postfix decrement.
Check operator -- (int)
{
Check temp;
temp.i = i--;
return temp;
}
void Display()
{ cout << "i = "<< i <<endl; }
};
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
// Operator function is called, only then value of obj is assigned to obj1
obj1 = --obj;
obj.Display();
obj1.Display();
// Assigns value of obj to obj1, only then operator function is called.
obj1 = obj--;
obj.Display();
obj1.Display();
return 0;
}