Friday, January 24, 2014

C++ Program to Perform Increment and Decrement Operations using Unary Operator Overloading

with 0 Comment
UNARY OPERATOR OVERLOADING 
AIM:
  • To Perform increment and decrement operations using unary operator overloading.
ALGORITHM:
1) Start the program.
2) Create Class unary with one constructors.
3) The constructor takes no arguments and is used to create objects that are
not initialised.
4) Declare a function operator ‘++’ and ‘-- inside the class unary which are the
function where overloading is done.
5) Create an object for the class.
6) Call the functions with declared object.
7) Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class unary
{
private:
int x,y,z;
public:
unary(void)
{
cout<< "Enter Any Three Integer Nos. : ";
cin>>x>>y>>z;
}
void display(void)
{
cout<< endl<< " The Three Nos. Are : "<< x<< " , "<< y<< " , "<< z;
}
void operator --()
{
x = --x;
y = --y;
z = --z;
}
void operator ++()
{
x = ++x;
y = ++y;
z = ++z;
}
};
void main()
{
clrscr();
unary s;
s.display();
--s;
cout<< endl<< endl<< endl<< "******* The Decremented Values ********"<< endl;
s.display();
++s;
cout<< endl<< endl<< endl<< "******* The Incremented Values ********"<< endl;
s.display();
cout<< endl<< endl<< "***************************************";
getch();
}

Output:
Enter Any Three Integer Nos. :
4 7 9
The Three Nos. Are : 4 , 7 , 9
******* The Decremented Values ********
The Three Nos. Are : 3 , 6 , 8
******* The Incremented Values ********
The Three Nos. Are : 4 , 7 , 9

RESULT:
  • Thus, The given program for unary operator overloading has been written and executed successfully.

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive