In programming (Java, C, C++, PHP etc. ), increment ++ operator increases the value of a variable by 1
#include<iostream>
using namespace std;
class inc
{
int a;
public:
inc(int x=0)
{
a=x;
}
inc operator++()
{
++a;
return *this;
}
inc operator++(int)
{
inc temp=*this;
a++;
return temp;
}
void display()
{
cout<<“Value : “<<a<<endl;
}
};
int main()
{
inc I1(13),I2;
cout<<“Before pre-incrementing “;
I1.display();
++I1;
cout<<“After pre-incrementing “;
I1.display();
cout<<“Before post-incrementing “;
I1.display();
I2=I1++;
cout<<“After post-incrementing “;
I1.display();
return 0;
}
OUTPUT:
Suppose, if x = 6 then,
++a; //x becomes 7
a++; //x becomes 8
--a; //x becomes 7
a--; //x becomes 6
Operator Increment using C++#include<iostream>
using namespace std;
class inc
{
int a;
public:
inc(int x=0)
{
a=x;
}
inc operator++()
{
++a;
return *this;
}
inc operator++(int)
{
inc temp=*this;
a++;
return temp;
}
void display()
{
cout<<“Value : “<<a<<endl;
}
};
int main()
{
inc I1(13),I2;
cout<<“Before pre-incrementing “;
I1.display();
++I1;
cout<<“After pre-incrementing “;
I1.display();
cout<<“Before post-incrementing “;
I1.display();
I2=I1++;
cout<<“After post-incrementing “;
I1.display();
return 0;
}
OUTPUT:
Before pre-incrementing Value : 13
After pre-incrementing Value : 14
Before post-incrementing Value : 14
After post-incrementing Value : 15
tags : c , programs of c , c++ , programs of c++ , tutorialspoint , operator increment
After pre-incrementing Value : 14
Before post-incrementing Value : 14
After post-incrementing Value : 15
tags : c , programs of c , c++ , programs of c++ , tutorialspoint , operator increment