#include #include using namespace std; struct inventory_item { string name; int number; double cost; int num_in_inventory; }; void print_item(const inventory_item * item); bool sell_item(inventory_item * to_sell); void main() { //dynamically allocate memory for an //inventory_item and set its values inventory_item * shirts = new inventory_item; shirts->name = "Shirts"; shirts->number = 1232; shirts->cost = 20.00; shirts->num_in_inventory = 10; //print the new item print_item(shirts); //sell one of the new item sell_item(shirts); //print the item again print_item(shirts); //create an array of pointers //to inventory_items //the array itself is statically //allocated (NOT on the heap) //each slot in the array //points to an inventory_item //we must dynamically allocate //space for each item (on the heap) inventory_item * items[2]; items[0] = new inventory_item; items[0]->name = "Hats"; items[0]->number = 1234; items[0]->cost = 6.99; items[0]->num_in_inventory = 15; items[1] = new inventory_item; items[1]->name = "Jackets"; items[1]->number = 1235; items[1]->cost = 59.99; items[1]->num_in_inventory = 20; print_item(items[0]); sell_item(items[0]); print_item(items[0]); //dynamically allocate an array //of inventory_item objects inventory_item* dyn_items; dyn_items = new inventory_item[2]; dyn_items[0].name = "Coats"; dyn_items[0].number = 9876; dyn_items[0].cost = 159.99; dyn_items[0].num_in_inventory = 5; dyn_items[1].name = "Boots"; dyn_items[1].number = 9874; dyn_items[1].cost = 129.99; dyn_items[1].num_in_inventory = 21; print_item(&dyn_items[1]); sell_item(&dyn_items[1]); print_item(&dyn_items[1]); //dynamically allocate an array //of pointers to inventory_item //objects //the array is dynamically allocated //as are the objects pointed to by //each element of the array inventory_item ** double_dyn_items; double_dyn_items = new inventory_item*[2]; double_dyn_items[0] = new inventory_item; double_dyn_items[0]->name = "Pants"; double_dyn_items[0]->number = 7896; double_dyn_items[0]->cost = 69.99; double_dyn_items[0]->num_in_inventory = 4; double_dyn_items[1] = new inventory_item; double_dyn_items[1]->name = "Skirts"; double_dyn_items[1]->number = 7833; double_dyn_items[1]->cost = 49.99; double_dyn_items[1]->num_in_inventory = 34; print_item(double_dyn_items[0]); sell_item(double_dyn_items[0]); print_item(double_dyn_items[0]); delete shirts; for (int i = 0; i < 2; i++) { delete items[i]; } delete[] dyn_items; for (int j = 0; j < 2; j++) { delete double_dyn_items[j]; } delete[] double_dyn_items; //use the variable shirts again //by dynamically allocating a //new block of space shirts = new inventory_item; shirts->name = "New_Shirts"; shirts->number = 5736; shirts->cost = 54.33; shirts->num_in_inventory = 96; print_item(shirts); delete shirts; } /* A function to print all info about an item. input - the item to print output - none */ void print_item(const inventory_item * item) { cout << "Item: " << item->name << endl; cout << "Item Number: " << item->number << endl; cout << "Cost: " << item->cost << endl; cout << "Number Remaining: " << item->num_in_inventory << endl; } /* A function to sell an item by reducing the number in the inventory by 1. input - reference to an inventory_item output - true if item was sold, false otherwise What would happen if you left off the &? What would happen if you added const? */ bool sell_item(inventory_item * to_sell) { bool sell_ok = false; if(to_sell->num_in_inventory > 0) { to_sell->num_in_inventory = to_sell->num_in_inventory - 1; sell_ok = true; } else { sell_ok = false; } return sell_ok; }