Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #define SIZE 12
- using namespace std;
- template < class T >
- class InventoryItem {
- private:
- T m_Item;
- float m_Value;
- public:
- InventoryItem(T inItem, float inVal){
- m_Item = inItem;
- m_Value = inVal;
- };
- //get functions return member values
- T getItem(){
- return m_Item;
- };
- float getValue(){
- return m_Value;
- };
- };
- //part b
- template <class T>
- class Inventory{
- private:
- InventoryItem<T> *m_pIvtItems[SIZE];
- int m_Pos;
- public:
- Inventory(){
- m_Pos = 0;
- }
- void Insert(InventoryItem<T> *pInput){
- if (m_Pos > SIZE){
- throw string("Array is Full");
- }
- m_pIvtItems[m_Pos] = pInput;
- m_Pos++;
- }
- void Print(){
- for (int i = 0; i < m_Pos; i++){
- cout << m_pIvtItems[i]->getItem() << ":";
- cout << m_pIvtItems[i]->getValue() << endl;
- }
- }
- };
- int main(){
- Inventory <string> inv;
- string itemName; float value;
- InventoryItem <string> *pItem;
- cout << "Enter an item: ";
- cin >> itemName;
- while (itemName != "End"){
- cout << "Enter item value: ";
- cin >> value;
- pItem = new InventoryItem<string>("itemName", value);
- try {
- inv.Insert(pItem);
- }
- catch (string err){
- cout << err << endl;
- delete pItem;
- }
- cout << "Enter an item: ";
- cin >> itemName;
- }
- cout << endl << "Inserted items: " << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement