Advertisement
TheWhiteFang

Q3 past yr MT (UML x Template)**

Jan 24th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #define SIZE 12
  4.  
  5. using namespace std;
  6.  
  7. template < class T >
  8. class InventoryItem {
  9.    
  10. private:
  11.     T m_Item;
  12.     float m_Value;
  13.  
  14. public:
  15.     InventoryItem(T inItem, float inVal){
  16.         m_Item = inItem;
  17.         m_Value = inVal;
  18.     };
  19.     //get functions return member values
  20.     T getItem(){
  21.         return m_Item;
  22.     };
  23.  
  24.     float getValue(){
  25.         return m_Value;
  26.     };
  27. };
  28.  
  29. //part b
  30.  
  31. template <class T>
  32. class Inventory{
  33. private:
  34.     InventoryItem<T> *m_pIvtItems[SIZE];
  35.     int m_Pos;
  36.  
  37. public:
  38.     Inventory(){
  39.         m_Pos = 0;
  40.     }
  41.  
  42.     void Insert(InventoryItem<T> *pInput){
  43.    
  44.         if (m_Pos > SIZE){
  45.             throw string("Array is Full");
  46.         }
  47.         m_pIvtItems[m_Pos] = pInput;
  48.         m_Pos++;
  49.     }
  50.  
  51.     void Print(){
  52.         for (int i = 0; i < m_Pos; i++){
  53.             cout << m_pIvtItems[i]->getItem() << ":";
  54.             cout << m_pIvtItems[i]->getValue() << endl;
  55.         }
  56.     }
  57. };
  58.  
  59. int main(){
  60.  
  61.     Inventory <string> inv;
  62.     string itemName; float value;
  63.     InventoryItem <string> *pItem;
  64.    
  65.    
  66.  
  67.         cout << "Enter an item: ";
  68.         cin >> itemName;
  69.  
  70.         while (itemName != "End"){
  71.  
  72.             cout << "Enter item value: ";
  73.             cin >> value;
  74.  
  75.             pItem = new InventoryItem<string>("itemName", value);
  76.             try {
  77.                 inv.Insert(pItem);
  78.             }
  79.             catch (string err){
  80.                 cout << err << endl;
  81.                 delete pItem;
  82.             }
  83.             cout << "Enter an item: ";
  84.             cin >> itemName;
  85.         }
  86.         cout << endl << "Inserted items: " << endl << endl;
  87.        
  88.         return 0;
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement