So, i have defined a father class of grocery items that have the attributes of the items such as price name and quantity. I was also asked to make a subclass for each category of groceries, such as dairy, cereal and such. I need to make a program that lets a the user populate a list (i think a vector would be the best choice) after the list is populated the user has to be able to change the quantity of each item.
I researched and since they are all different classes and dont all have the same attributes im going to need to use the pointer to point to that class then push it back, right? but im having a hard time coming up with the code.
i figured for the user end it will first ask u if u want to add an item. then what kind and then ask u to type in all the attributes. but im suppose to have one add function for all the classes.
so i fiured it would be something for the dairy i set the pointer to the dairty and then push back (new.dairy)?
but its the part where i actually fill in the attributes that im stuck in. I havent programmed in years so, im sure im missing the basics of it..
would i set a point of item* class (the base class) to p
then proceed to get user input to populate the vector
If setPrice, setName and printIt will do the same for all the derived class, put the code in the base class. There will be no need for that methods to be declared virtual.
You could also use the constructor of the base class
1 2 3 4
GroceryItems::GroceryItems(){
price = 0.0;
name = ' ';
}