home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / inv_item.cpp < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  58 lines

  1. //                  inv_item.cpp
  2.  
  3. // Implementation of inv_item class
  4. #include <iostream.h>
  5. #include <string.h>
  6. #include "invItem.h"
  7.  
  8. invItem::invItem()                                     // Note 1
  9. {
  10.     id = new char[5];
  11.     strcpy( id, "####" );
  12.     num_available = 0;
  13. }
  14.  
  15. invItem::invItem( const invItem& item )              // Note 2
  16. {
  17.     id = new char[ strlen( item.id ) + 1 ];
  18.     strcpy( id, item.id );
  19.     num_available = item.num_available;
  20. }
  21.  
  22. invItem::invItem( char *the_id, int the_num )        // Note 3
  23. {
  24.     id = new char[ strlen( the_id ) + 1 ];
  25.     strcpy( id, the_id );
  26.     num_available = the_num;
  27. }
  28.  
  29. invItem::~invItem()                                   // Note 4
  30. {
  31.     delete[] id;                                            
  32. }
  33.  
  34. bool invItem::changeNum( int change_amt )                                    
  35. {
  36.     if ( num_available + change_amt >=0 ) {
  37.         num_available += change_amt;
  38.         return true;
  39.     }
  40.     return false;
  41. }
  42.  
  43. bool invItem::changeId( char *newId )                // Note 5
  44. {
  45.     char *tmp = new char[ strlen( newId ) + 1 ];
  46.     strcpy( tmp, newId );
  47.     delete[] id;
  48.     id = tmp;
  49.     return true;
  50. }
  51.  
  52. void invItem::display()
  53. {
  54.     cout << "Id: " << id
  55.          << " - Available " << num_available
  56.          << endl;
  57. }
  58.