Sunday, July 4, 2010

PREFIX & POSTFIX EVALUATION

Buzz It

/*PROGRAM TO INCREMENT A VALUE BY OVERLOADING ++
OPERATOR(PREFIX & POSTFIX )*/


#include<iostream.h>
#include<conio.h>
class number
{
int a,temp;
public:
number();
int display();
number operator++(void);
number operator++(int);
};
number::number()
{
a=0;
}
int number::display(void)
{
return a;
}
number number::operator++(void)
{
number temp;
temp.a=++a;
return temp;
}
number number::operator++(int)
{
number temp;
temp.a=a++;
return temp;
}
void main()
{
number s1,s2;
clrscr();
cout<<"s1="<<s1.display()<<"and s2="<<s2.display();
++s1;
s1++;
s2=s1++;
cout<<"\nafter applying increment operator\n";
cout<<"s1="<<s1.display()<<"and s2="<<s2.display();
getch();
}


OUTPUT:
s1=0 and s2=0
after applying increment operator
s1=3 and s2=2

0 comments:

Post a Comment