-3

I’m talking about line 4 after public:

class Stack {
        int top;
     
    public:
        int a[MAX]; // Maximum size of Stack
     
        Stack() { top = -1; }
        bool push(int x);
        int pop();
        int peek();
        bool isEmpty();
    };
     
    bool Stack::push(int x)
    {
        if (top >= (MAX - 1)) {
            cout << "Stack Overflow";
            return false;
        }
        else {
            a[++top] = x;
            cout << x << " pushed into stack\n";
            return true;
        }
    }

UPDATE: Because others have commented on it, I just wanted to clarify that I know what an array is and how to use one and I have read documentations before. I just needed help in understanding how it is used inside classes. Also, I would like to thank the people who helped me by answering the question. I appreciate it very much!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Unyaya
  • 68
  • 7
  • 4
    If you don't recognize an array declaration, I think you need to go through proper C++ tutorial, start to finish... The entire rest of your code is surely equally mysterious. – ShadowRanger Jan 08 '21 at 06:01
  • 2
    It declares an *array*. Seriously if you have got this far in C++ without seeing an array before then you aren't learning in a good way. Get yourself a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – john Jan 08 '21 at 06:02
  • It depends on how `MAX` was defined. You didn't expose the definition of `MAX`. – Scheff's Cat Jan 08 '21 at 06:02
  • 3
    I don't understand "_What does `int a[MAX]` do and what does it mean_" and then "_I know what an array is and how to use one ... I just needed help in understanding how it is used inside classes_" - It's used in the same way as when used outside classes. – Ted Lyngmo Jan 08 '21 at 06:55
  • 2
    “*I just needed help in understanding how it is used inside classes*” - an array works EXACTLY THE SAME inside a class as it does outside of a class. – Remy Lebeau Jan 08 '21 at 07:16

3 Answers3

1

This declares an array a which can hold up to MAX number of elements, which in this case are integers since this is an integer array.

Edmund
  • 332
  • 1
  • 9
  • 1
    The array contains *exactly* `MAX` elements, as arrays have fixed size in C++. – drRobertz Jan 08 '21 at 11:03
  • You are correct. I meant that not all the elements in the array needed to be assigned a value. I should have said `hold up to MAX number of values`. – Edmund Jan 08 '21 at 11:26
  • As this is a beginner's question, that phrasing may also be misleading and confusing. An array always contains the same number of objects. Their values may be undefined if not intialized or assigned, but the array object has a fixed size. – drRobertz Jan 08 '21 at 13:42
1

What does int a[MAX] do and what does it mean

It is declaration of a variable whose name is a and type is int[MAX] which is an array of MAX number of int elements.

I just needed help in understanding how it is used inside classes

When variable declaration is inside a class, it declares a member variable.

eerorika
  • 232,697
  • 12
  • 197
  • 326
-1

There are two (main) types of arrays in C++,

  • Static Arrays (which are resolved at compile time).
  • Dynamic Array (which are resolved at runtime).

int a[MAX]; is the way to create a static array. For this the size must be known at compile time. And these arrays are fast to create and are destroyed once gone out of scope.

Dynamic arrays are created at runtime. These data structures must be destroyed manually (these aren't automatically destroyed once gone out of scope). And allocating them is a little slow compared to creating a static array. To create a dynamic array, you can use either new[]/ delete[] or use malloc()/ free(). The same array can be implemented as a dynamic array like so:

class Stack {
        int top;
     
    public:
        int* a = nullptr;
     
        Stack() { top = -1; a = new[MAX]; }
        ~Stack() { delete[] a; } // Make sure to delete the allocated memory or youll end up with a memory leak.
        bool push(int x);
        int pop();
        int peek();
        bool isEmpty();
    };
     
    bool Stack::push(int x)
    {
        if (top >= (MAX - 1)) {
            cout << "Stack Overflow";
            return false;
        }
        else {
            a[++top] = x;
            cout << x << " pushed into stack\n";
            return true;
        }
    }

Links: What is an array?

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • 3
    Please don't use or suggest using `malloc`/`free` in C++, that's a huge pitfall for people who don't know exactly what they are doing. In modern C++ you should even avoid `new`/`delete`, but only use standard containers. Also you might want to add a little warning that `Stack` needs to implement the rule of 5/3/0. – Lukas-T Jan 08 '21 at 07:45