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
Sign In
Register
AdSense Revenue
Active Members
Today
Asheej T K
(181)
Alwyn
(116)
Manindra Upadh...
(76)
Last 7 Days
Alwyn
(1038)
Asheej T K
(754)
Manindra Upadh...
(542)
more...
Resources
»
Code Snippets
»
General
»
Unary - operator Overloading in c++
Posted Date: 24 Aug 2009
Resource Type:
Code Snippets
Category:
General
Author:
Santosh Kumar Singh
Member Level:
Gold
Rating:
Points
: 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 2009
Member 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
Constructor with reference initializers in c++
Example of New operator overloading in c++
Overloadeing new operator for the Names class using c++
Deletes all array elements in c++
Static list Head and tail pointer in c++
Add the entry to the linked list using c++
dotNet Slackers
About Us
Contact Us
Privacy Policy
Terms Of Use