home *** CD-ROM | disk | FTP | other *** search
- // ex08007.cpp
- // Overloaded unary minus
- #include <iostream.h>
- #include <string.h>
-
- class ItemQty {
- int onhand;
- char desc[25];
- public:
- ItemQty(int oh, char *d) { onhand = oh; strcpy(desc, d); }
- void display() { cout << '\n' << desc << ": " << onhand; }
- // ---- overloaded unary - operator
- int operator-() { return -onhand; }
- };
-
- main()
- {
- ItemQty item1(100, "crankshaft");
- ItemQty item2(-50, "driveshaft");
- item1.display();
- cout << '\n' << -item1; // invoke the overloaded -
- item2.display();
- cout << '\n' << -item2; // invoke the overloaded -
- }