// Program to illustrate the overloading of
// operators ++ and -- (both prefix and postfix)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class number
{
private:
int x,y;
public:
number()
{
x=y=0;
}
number(int a,int b)
{
x=a;
y=b;
}
number operator++() // Prefix Increment Operator
{
return number(++x,++y);
}
number operator++(int) // Postfix Increment Operator
{
return number(x++,y++);
}
number operator--() // Prefix Decrement Operator
{
return number(--x,--y);
}
number operator--(int) // Postfix Decrement Operator
{
return number(x--,y--);
}
void show()
{
cout<<" x = "<<x<<" "<<"y = "<<y<<endl;
}
};
void main()
{
clrscr();
number n1,n2(5,33);
cout<<endl<<"Initialization"<<endl;
cout<<"n1";
n1.show();
cout<<"n2";
n2.show();
n1=n2++;
cout<<endl<<"n1=n2++ "<<endl;
cout<<"n1";
n1.show();
cout<<"n2";
n2.show();
n1=n2--;
cout<<endl<<"n1=n2-- "<<endl;
cout<<"n1";
n1.show();
cout<<"n2";
n2.show();
n1=++n2;
cout<<endl<<"n1=++n2 "<<endl;
cout<<"n1";
n1.show();
cout<<"n2";
n2.show();
n1=--n2;
cout<<endl<<"n1=--n2 "<<endl;
cout<<"n1";
n1.show();
cout<<"n2";
n2.show();
getch();
}
/* OUTPUT
Initialization
n1 x = 0 y = 0
n2 x = 5 y = 33
n1=n2++
n1 x = 5 y = 33
n2 x = 6 y = 34
n1=n2--
n1 x = 6 y = 34
n2 x = 5 y = 33
n1=++n2
n1 x = 6 y = 34
n2 x = 6 y = 34
n1=--n2
n1 x = 5 y = 33
n2 x = 5 y = 33
*/
Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.
