C++ program for overloading the unary operator ++.

ALGORITHAM:
• Start the process
• Invoke the class counter
• Crate two objects c1 and c2
• Assign values to c1 an c2
o Call c1.get_count()
o Call c2.get_count()
• Increment the values
o C1++
o C2++
o ++c2
• Print c1 and c2
• Stop the process

PROGRAM

#include<iostream.h>
#include<conio.h>
class counter
{
int count;
public:
counter()
{
	count=0;
}
int get_count()
{
return count;
}
void operator++()
{
count++;
}
};
 
void main()
{
counter c1,c2;
cout<<"\nC1 ="<<c1.get_count();
cout<<"\nC2 ="<<c2.get_count();
 
c1++; 		//Using overloaded ++ operator.
c2++;
++c2;
cout<<"\nC1 ="<<c1.get_count();
cout<<"\nC2 ="<<c2.get_count();
getch();
}

OUT PUT:
C1=0 C2=O
C1=1 C2=2

One Response to “C++ program for overloading the unary operator ++.”

Leave a Reply