C++ Programming

C++ Program to Implement Unary Operator Overloading

Write a menu driven program to implement the concept of unary operator overloading.

Source Code

#include<iostream>

#include<conio.h>

using namespace std;

class oper

{

private:

int count;

public:

oper()

{

count=0;

}

int getcount()

{

return count;

}

void operator ++()

{

count++;

}

void operator --()

{

count--;

}

void give()

{

cout<<"The new count is: "<<count;

}

};

int main()

{

oper n1, n2;

int i;

do{

cout<<endl<<"1.Increment"<<endl<<"2.Decrement"<<endl<<"3.Exit"<<endl<<"Enter your choice"<<endl;

cin>>i;

if(i==1)

{

++n1;

n1.getcount();

n1.give();

}

else if(i==2)

{

--n2;

n2.getcount();

n2.give();

}

}while(i!=3);

getch();

}

Output

Back to top button