writing a stack with an array of static or dynamic

I can write either static or dynamic; my choice. This is to have an upper limit, of apparently my choosing, on the maximum size the stack can have. I need to create a member function named full() inside of a stack class. The member function full is to return true or false according to whether or not the array used to store the stack elements is full. This below is my code and I'm quite sure it's at least a bit off, semantically and the logic behind it. I was wondering if I could get a tip on how to correct course. I know that true or false immediately compels me to use boolean keyword and I also want some kind of conditional "framework" for the function with maybe a loop, "if, else". This isn't to be an entire program, just a small segment.

pseudo header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class stack
{
public:
	stack();
	bool full(int top[5]);
	~stack();
};

    
//functioned defined or "cpp.file"
     bool stack::full(int a[5])
    {
	for (int i = 0; i < 5; i++) {
		if a >= [5]
			return true;
    }
	return false;
Last edited on
Since you passed an int array to full(), I'm presuming you're trying to represent a stack of ints. Your array of ints and a counter of the number of entries used need to be member variables of the class.

Here's a simple example of an int stack.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class stack
{   
public:
    static const int MAX_ENTRIES = 5;

    stack();
    ~stack();
    bool full () const;
private:
    int     arr[MAX_ENTRIES];  // static int array
    int     num_entries = 0;  // number of entries used
};

bool stack::full () const
{   return num_entries == MAX_ENTRIES;  // true if stack is full
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
thank you very much!
Topic archived. No new replies allowed.