home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX08007.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  583 b   |  25 lines

  1. // ex08007.cpp
  2. // Overloaded unary minus
  3. #include <iostream.h>
  4. #include <string.h>
  5.  
  6. class ItemQty {
  7.     int onhand;
  8.     char desc[25];
  9. public:
  10.     ItemQty(int oh, char *d) { onhand = oh; strcpy(desc, d); }
  11.     void display() { cout << '\n' << desc << ": " << onhand; }
  12.     // ---- overloaded unary - operator
  13.     int operator-() { return -onhand; }
  14. };
  15.  
  16. main()
  17. {
  18.     ItemQty item1(100, "crankshaft");
  19.     ItemQty item2(-50, "driveshaft");
  20.     item1.display();
  21.     cout << '\n' << -item1;        // invoke the overloaded -
  22.     item2.display();
  23.     cout << '\n' << -item2;        // invoke the overloaded -
  24. }
  25.