we are going to make an Inventory Item a base class and make it simpler for you to transition into inheritance by creating two new child classes.
Create two new classes named ClothingItem and FoodItem. Both should inherit from InventoryItem. Don’t worry about adding any properties just yet, we’ll get to that in the next verse.
Ensure InventoryItem has a pure virtual public method defined to display the details about an InventoryItem.
In ClothingItem and FoodItem, override the display method in InventoryItem class. Ensure the output to the user includes detail indicating if the item is a Clothing or Food item.
When gathering input from the user, add an additional step at the beginning to ask them if their inventory item is a Clothing or Food item. Don’t worry about supporting any other items right now. Force them to select one or the other. Also, feel free to use integers to help here. You can ask the user, Press 1 for Clothing Item or Press 2 for Food item. However, if they enter an invalid option inform them of the error and keep asking until the user selects Clothing or Food item.
Based-on if the user selected a Clothing or Food item, instantiate the corresponding class and store it into the dynamic array of InventoryItems. You must use the new keyword here. I want to see dynamic allocation of the array and the Clothing or Food Item. Also, this will make your array a dynamic array of InventoryItems and an array of pointers. We call this a double pointer. You will need to declare the double pointer like this InventoryItem **inventoryItems = new InventoryItem*[userSizeInput];
Loop the dynamic array of InventoryItems and call the display method of each item.
Cleanup all memory you’ve allocated. loop the dynamic array and destroy each array item first. Then once the loop is finished, destroy the dynamic array.
Ensure you use meaningful names and that your code is pretty formatted. Feel free to use any of the three methods to implementing classes. For this, I would suggest 4 files. One file for the InventoryItem class. One file for the ClothingItem class. One file for the FoodItem class. One file for the main method. Feel free to implement the classes inside or outside of the class definition, which ever is easier for you I am using visual studio 2019 85% of this material is already done all that is left is to do is add while loop, create classes themselves at least 2 change it to pure virtual storing multiple child type as 1 type


0 comments