[C++] Building a stack class 12-13-2014, 06:50 AM
#1
Alright, if you havn't already done so, go read the thread on creating your first stack (in C) here as I won't be explaining some of the basic concepts in this thread.
Another note: this one will not be as spoonfed as the last one, I assume that you understood most of it, I will, however go into some of the concepts again as things will be a touch different.
Let's get started. What are we going to need to make this class?
Pretty much the same as in C. lets break this down one by one.
1. type of data
For this class, we will also be using ints, I will /probably/ write a tutorial on how to make template classes, as well as one explaining how to make one that uses multiple POD types.
2. some storage
Since we are using a class, everything can be embodied into the class instead of a struct (note to experienced programmers: although classes use structs to store member data anyways)
We will use an array again to store them (fixed size), you can use resizing or linked lists if you want variable stacks, but that defeats the point (congrats, you made a vector)
3. methods to manage the stack
We will need pretty much the same methods as the C stack (init, push, pop, destroy) but we can add a few. Here is the full list:
The new ones:
isEmpty will be a boolean that tells the user if the stack has zero elements in it (useful for removing unused items or backwards sorting)
count will just return the number of elements in the stack.
Okay, so let's write the header for our class incorporating points 2 and 3.
NOTE: I assume you have a basic understanding of how c++ classes work. If you do not, google "learn C++ the hard way"
Now we can get on to our code file. This will be mostly the same as the C file, so I won't spoonfeed it all to you.
Now, that is a considerable bit less code than the C variant, but EVERYTHING is the same. Usage is a little different.
This seems REALLY simple, so I won't bother with a poll here. I personally like the C version better, but it can EASILY be adapted to a class.
If you need help understanding this, feel free to ask questions.
Another note: this one will not be as spoonfed as the last one, I assume that you understood most of it, I will, however go into some of the concepts again as things will be a touch different.
Let's get started. What are we going to need to make this class?
- type of data
- some storage
- methods to manage the stack
Pretty much the same as in C. lets break this down one by one.
1. type of data
For this class, we will also be using ints, I will /probably/ write a tutorial on how to make template classes, as well as one explaining how to make one that uses multiple POD types.
2. some storage
Since we are using a class, everything can be embodied into the class instead of a struct (note to experienced programmers: although classes use structs to store member data anyways)
We will use an array again to store them (fixed size), you can use resizing or linked lists if you want variable stacks, but that defeats the point (congrats, you made a vector)
3. methods to manage the stack
We will need pretty much the same methods as the C stack (init, push, pop, destroy) but we can add a few. Here is the full list:
- init
- push
- pop
- isEmpty
- count
- destroy
The new ones:
isEmpty will be a boolean that tells the user if the stack has zero elements in it (useful for removing unused items or backwards sorting)
count will just return the number of elements in the stack.
Okay, so let's write the header for our class incorporating points 2 and 3.
Code:
class myStack
{
private:
int *data;
int sp; //short for stack pointer
public:
myStack(int); //the same as init, takes 1 argument: the size of the stack
void push(int);
int pop();
bool isEmpty();
int count();
~myStack(); //the same as destroy, but is called automatically
};NOTE: I assume you have a basic understanding of how c++ classes work. If you do not, google "learn C++ the hard way"
Now we can get on to our code file. This will be mostly the same as the C file, so I won't spoonfeed it all to you.
Code:
#include "myStack.h"
myStack::myStack(int size)
{
assert(size > 1); //don't create a class, the programmer is an idiot.
data = new int[size]; //in C++ we don't need to sizeof, since new does all that for us
sp = 0;
}
myStack::~myStack() //yes, I know it is out of order. personal preference
{
//remember, we never call ~myStack, it happens on its own
delete data; //delete works similar to free.
}
void myStack::push(int input)
{
data[sp++] = input;
/* if you don't understand this, use this one instead
* int sp_tmp = sp;
* data[sp_tmp] = input;
* ++sp; //update the pointer
*/
}
int myStack::pop()
{
return data[--sp];
/* or
* int sp_tmp = sp - 1; //dont forget the -1
* int data_tmp = data[sp];
* --sp;
* return data_tmp;
*/
}
bool myStack::isEmpty()
{
return (sp == 0);
/* or
* if (sp == 0)
* return true;
* return false;
*/
}
int myStack::count()
{
return sp; //don't get in the habbit of this, it WILL not work on real stacks, but this is C++
}Now, that is a considerable bit less code than the C variant, but EVERYTHING is the same. Usage is a little different.
Code:
#include "myStack.h"
int main()
{
myStack sample_stack(5); //create a stack that can hold 5 elements
int catch;
sample_stack.push(4);
sample_stack.push(3);
sample_stack.push(2);
sample_stack.push(1);
while (! sample_stack.isEmpty())
catch = sample_stack.pop();
//note, we dont need to call ~myStack
return 0;
}This seems REALLY simple, so I won't bother with a poll here. I personally like the C version better, but it can EASILY be adapted to a class.
If you need help understanding this, feel free to ask questions.

























![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)
tack, which is a templated type, and a few others with similar functionality including std::deque..
.