I have attempted to google this but haven't had any luck. Is it possible to create a stack of a list? I am just trying to get into lists and being able to push/pop items.
Right now I have a little program that has a class Inventory which allows the user to add a new item (with name, price, and quantity) and then output it to a file. Any tips on how to convert this to using a stack? Should I be using a struct?
I think I tried to combine stacks and linked lists. I would like to make a stack class that is able to push or pop items from the "inventory." Would I be able to just make an array of my inv object and then make a push/pop function for it?
Did you look at the std::stack documentation? It does offer push/pop. It does use some other container, like std::list, for actual storage under its hood.
If you just need to use a stack, then the standard container does the work.
If you need to learn the theory of algorithms and data structures, then looking at the std::stack documentation says: "yes, it is possible."
struct Gun
{
string gunName;
double gunPrice;
double FPS;
int qty;
// other vars a gun has
};
class Inventory
{
public:
void CreateWeapons(args, int slot)
{
w[slot].gunName = arg1;
// etc etc
}
private:
Weapon w[2]; // Can have 2 weapons || A pointer can be created here to create new guns whenever
}; // Just make sure you delete[] w in the deconstructor. :)
There is no difference between class and struct in C++ other than the default privacy settings. I use struct 100% of the time in my code just to go against the norm.
Ah, my misunderstanding. I'm sure I read that only classes could have functions ( Why people use them ). As I did C before this my heart always tells me to put like data in a struct, and coming into C++ tells me to have a struct of data in a class.