Making a stack of a class?

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?


class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Inventory
{
	private:
		string gunName;
		double gunPrice;
		int gunQty;

	public:
		Inventory();

		void newItem(string n, double p, int q);
		void outputToFile(ofstream& out);

		~Inventory();
};

Inventory::Inventory()
{
	gunName = "";
	gunPrice = 0.0;
	gunQty = 0;
}

void Inventory::newItem(string n, double p, int q)
{
	gunName = n;
	gunPrice = p;
	gunQty = q;
}

void Inventory::outputToFile(ofstream& out)
{
	out << gunName << " " << gunPrice << " " << gunQty << endl;	
}

Inventory::~Inventory()
{

}


Driver:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
	string n;
	double p;
	int q, amount;


	Inventory inv;
	ofstream outFile;
	outFile.open("Inventory.txt");

	cout << "Enter amount of entries: ";
	cin >> amount;

	for (int i = 0; i < amount; i++)
	{
		cout << "\n\nEnter gun name, gun price, and gun qty: ";
		cin >> n >> p >> q;
		
		inv.newItem(n,p,q);
		inv.outputToFile(outFile);
	}

	outFile.close();

	system("PAUSE");
	return 0;
}


Thank you for any info.

Edit: I apologize, this was meant to be on the Beginners Forum. I don't know how I got here.
Last edited on
What do you mean by "stack" and "stack of a list"?
This? http://www.cplusplus.com/reference/stack/stack/


PS. I think you can still edit your first post and that it has somewhere a "change Forum" option.
Thank you, I moved it to Beginners Forum.

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."
Hmmm I struggle to understand your use of class here.

1
2
3
string gunName;
double gunPrice;
int gunQty;


Can be easily contained in a struct

for instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
Last edited on
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.

I don't know, I'm trying.
Topic archived. No new replies allowed.