Write a program of postfix and prefix unary increment operator


 

/* Write a program of postfix and prefix unary  increment operator*/

#include<iostream.h>

#include<conio.h>

class score

{

int val;

public:

score()

{

val=0;

}

score operator++()

{

score temp;

val=val+1;

temp.val=val;

return(temp);

}

score operator++(int)

{

score temp;

val=val+1;

temp.val=val;

return(temp);

}

int show()

{

return(val);

}};

void main()

{

clrscr();

score s1,s2;

cout<<“\n value of s1=”<<s1.show()<<endl;

cout<<“\n value of s2=”<<s2.show()<<endl;

s2=++s1;

cout<<“\n after prefix operator value of s1=”<<s1.show()<<endl;

cout<<“\n after prefix operator value of s2=”<<s2.show()<<endl;

s2=s1++;

cout<<“\n after postfix operator value of s1=”<<s1.show()<<endl;

cout<<“\n after postfix operator value of s2=”<<s2.show()<<endl;

getch();

}

/* output:

value of s1=0

value of s2=0

after prefix operator value of s1=1

after prefix operator value of s2=1

after postfix operator value of s1=2

after postfix operator value of s2=2*/

Related Posts

Comments are closed.