Source of iclass.cpp


  1: // Filenme: ICLASS.CPP
  2: // Purpose: Illustrates a simple class for representing an
  3: //          item in inventory.

  5: #include <iostream>
  6: #include <iomanip>
  7: using namespace std;


 10: class ItemType
 11: {
 12: public:
 13:     void Init(/* in */  int numberInStockNew,
 14:               /* in */  bool warrantyAvailableNew,
 15:               /* in */  double priceNew);
 16:     // Pre:  All in-parameters have been initialized.
 17:     // Post: The information in all in-parameters has been
 18:     //       transferred to the private data members of self.

 20:     void UpdateCount(/* in */ int numberInOrOut);
 21:     // Pre:  self and numberInOrOut have been initialized.
 22:     // Post: The number available of self has been updated by the
 23:     //       amount numberInOrOut, which may be positive or negative.

 25:     void DisplayInfo() const;
 26:     // Pre:  self has been initialized.
 27:     // Post: The information in all fields of item has been displayed.

 29: private:
 30:     int numberInStock;
 31:     bool warrantyAvailable;
 32:     double price;
 33: };

 35: void Pause(/* in */ int indentLevel);


 38: int main()
 39: {
 40:     cout << endl
 41:          << "This program uses a struct to "
 42:          << "represent an item in inventory. "   << endl
 43:          << "You should study the source code "
 44:          << "at the same time as the output. "   << endl << endl;

 46:     ItemType item;

 48:     item.Init(15, true, 29.95);
 49:     item.DisplayInfo();  cout << endl;
 50:     item.UpdateCount(7);
 51:     item.DisplayInfo();  cout << endl;
 52:     Pause(0);

 54:     item.Init(10, false, 45.50);
 55:     item.DisplayInfo();  cout << endl;
 56:     item.UpdateCount(5);
 57:     item.DisplayInfo();  cout << endl;
 58:     Pause(0);

 60:     item.Init(35, true, 100.00);
 61:     item.DisplayInfo();  cout << endl;
 62:     item.UpdateCount(-15);
 63:     item.DisplayInfo();  cout << endl;
 64:     Pause(0);

 66:     return 0;
 67: }


 70: //******************************************************************
 71: void ItemType::Init(/* in */  int numberInStockNew,
 72:                     /* in */  bool warrantyAvailableNew,
 73:                     /* in */  double priceNew)
 74: // Pre:  All in-parameters have been initialized.
 75: // Post: The information in all in-parameters has been
 76: //       transferred to the private data members of self.
 77: {
 78:     numberInStock = numberInStockNew;
 79:     warrantyAvailable = warrantyAvailableNew;
 80:     price = priceNew;
 81: }


 84: //******************************************************************
 85: void ItemType::UpdateCount(/* in */ int numberInOrOut)
 86: // Pre:  self and numberInOrOut have been initialized.
 87: // Post: The number available of self has been updated by the
 88: //       amount numberInOrOut, which may be positive or negative.
 89: {
 90:     numberInStock += numberInOrOut;
 91: }


 94: //******************************************************************
 95: void ItemType::DisplayInfo() const
 96: // Pre:  self has been initialized.
 97: // Post: The information in all fields of item has been displayed.
 98: {
 99:     cout.setf(ios::fixed, ios::floatfield);
100:     cout.setf(ios::showpoint);
101:     cout << setprecision(2);
102:     cout << "Number in stock:   "   << numberInStock << endl;
103:     cout << "Current price:     $"  << price         << endl;
104:     cout << "Warranty Avaiable: "
105:          << (warrantyAvailable ? "Yes" : "No")       << endl;
106: }


109: //******************************************************************
110: void Pause(/* in */ int indentLevel)
111: // Pre:  indentLevel has been initialized.
112: //       The input stream cin is empty.
113: // Post: The program has paused and displayed a one-line message
114: //       indented indentLevel spaces, after which the user has
115: //       pressed the ENTER key.
116: {
117:     cout << setw(indentLevel) << ""
118:          << "Press return to continue ... ";
119:     char returnChar;
120:     cin.get(returnChar);
121:     cout << endl;
122: }