++ unary operator program both postfix and prefix

oop

PROGRAM:

Define the ++ unary operator (both prefix and postfix) that should increment value of count to 1.

For example, the overloaded operator should allow the statements like: counter c1,c2;

c2=c1++;

c2=++c1

#include<iostream>using namespace std;

class counter

{

int count;

public:

counter()

{

count=0;

}

int get_count()

{

return count;

}

counter operator ++(int)

{

counter temp;

temp.count=count++;

return temp;

}

counter operator ++()

{

counter temp;

temp.count=++count;

return temp;

}

};

 

int main()

{

counter c1,c2;

cout<<“\nBefor increment\n”<<c1.get_count()<<“\n\n”;

cout<<“\nBefor increment\n”<<c2.get_count()<<“\n\n”;

c2=c1++;

c2=++c1;

cout<<“\nAfter increment\n”<<c1.get_count()<<“\n\n”;

cout<<“\nAfter increment\n”<<c2.get_count()<<“\n\n”;

return 0;

}

Leave a Reply to Anonymous Cancel reply