C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » General »

Unary - operator Overloading in c++


Posted Date: 24 Aug 2009    Resource Type: Code Snippets    Category: General
Author: Santosh Kumar SinghMember Level: Gold    
Rating: 1 out of 5Points: 5



////////////////////////////////////////
#include
#include

////////////////////////////////////////
// The ItemQty class.
////////////////////////////////////////
class ItemQty
{
int onhand;
char desc[25];

public:
ItemQty(int oh, char *d)
{ onhand = oh; std::strcpy(desc, d); }
void display() const
{ std::cout << '\n' << desc << ": " << onhand; }

// Overloaded unary - operator.
int operator-() const
{ return -onhand; }
};

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
ItemQty item1(100, "crankshaft");
ItemQty item2(-50, "driveshaft");

item1.display();
std::cout << '\n' << -item1; // invoke the overloaded -

item2.display();
std::cout << '\n' << -item2; // invoke the overloaded -

return 0;
}



Responses

Author: Miss Meetu Choudhary    27 Aug 2009Member Level: Diamond   Points : 2
Must add description to your code.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Unary - operator Overloading in c++  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Constructor with reference initializers in c++
Previous Resource: LinkedList Example using c++
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use