Templated Stack of 5 This is my first time using C++ Templates to make a data structure. I have written a program to implement a stack of up to 5 items.

#include&lt;iostream&gt;
#include&lt;iomanip&gt;

using namespace std;

template&lt;class T&gt;
class Queue{
private:
    T arrays[5];
    int i; //empty stack
public:
    Queue()
    {
        arrays[5] = {0}; //Empty stack
        i = -1; //Setting stack to be empty
    }
    T PUSH(T item)
    {
        if(i == 4)
        {
            cout &lt;&lt; "Stack is full" &lt;&lt; endl;
        }
        else
        {
        i++;
        arrays[i] = item;
        }
    }
    T POP()
    {
        if(i == -1)
        {
            cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
        }
        else
        {
        cout &lt;&lt; arrays[i] &lt;&lt; " has been popped" &lt;&lt; endl;
        i--;

        }
    }
    T TOP()
    {
        if(i == -1)
        {
            cout &lt;&lt; "Stack is empty" &lt;&lt; endl;
        }
        else
        {
        cout &lt;&lt; "Element at top of stack is " &lt;&lt; arrays[i] &lt;&lt; endl;
        }
    }

};


int main()
{
    Queue &lt;int&gt; Q;
    int option,push;
    do{
    cout &lt;&lt; "Select any of the following option" &lt;&lt; endl;
    cout &lt;&lt; "1.Push\n2.Pop\n3.Top\nPress any other button to exit" &lt;&lt; endl;
    cin &gt;&gt; option;

    switch(option)
    {
        case 1:
            cout &lt;&lt; "Which element to push" &lt;&lt; endl;
            cin &gt;&gt; push;
            Q.PUSH(push);
            break;
        case 2:
           Q.POP();
            break;
        case 3:
           Q.TOP();
            break;
        default:
            cout &lt;&lt; "Error!" &lt;&lt; endl;
            break;

    }
    }while(option &lt; 4);
return 0

}


Though I have got the program working. I would like to ask if there is any way I can improve on the code.
Queue != Stack

Queue behaviour is FIFO.  Stack behaviour is LIFO.  Currently your datastructure is called Queue, but it implements Stack, has stack error messages etc.  This is very confusing.

Magic Numbers

There are three main numbers you're using for manipulating your stack 5, 4 and -1.  Out of context, it's not immediately obvious what they mean.  Replacing them with constants would make the code easier to read.

Undefined behaviour

As has been hinted at by @Quuxplusone, this: 

_myarr[5]={0};


Isn't doing what you're expecting it to.  Rather than initializing all 5 of the elements in the array, which is what I suspect you expected, it is attempting to initialize the element with index 5.  Since arrays are index from 0 and it only contains 5 elements, you're overflowing the array.  This may or may not work, depending on how lucky you get.

Return Values

You've declared PUSH,POP,TOP all returning type T, however you're never returning anything.  Depending on your compiler + switches this may or may not actually compile.  PUSH probably doesn't need to return the value it's added anyway...

Console Output

Console output is great for testing your code to see if it's doing what you're expecting, however writing it into your classes gives the console access to private data members. Try to avoid putting console interaction in your data structures.  If you had written the console interactions outside of the class, you would have noticed that POP etc didn't return values a lot earlier in the cycle.

Error Conditions

If I pop from an empty stack, you write to the console, however the caller has no way to know that they haven't retrieved a value. Worse, if I push onto a full stack you simply throw the value away.  You should notify the user that there has been a failure, possibly by throwing an exception.

Exit

"Any other button" isn't a great hint that you need to press 4 or above to get out of the application.  Help your users, be explicit.

using namespace::std

Is generally frowned on, be explicit.

Indentation

Indentation matters, be consistent, it's a lot easier to read.

Putting it all together

I'm sure there's still room for improvement, however combining all of the above, the Stack would look a bit more like this:

#include&lt;iostream&gt;
#include&lt;iomanip&gt;
#include &lt;exception&gt;


template&lt;class T&gt;
class Stack {
private:
    static const int stack_size = 5;
    static const int stack_full = stack_size - 1;
    static const int stack_empty = -1;

    T arrays[stack_size] = {};
    int i = stack_empty;
public:
    void PUSH(T item)
    {
        if (i == stack_full)
        {
            throw std::runtime_error{ "Stack is full" };
        }

        i++;
        arrays[i] = item;
    }
    T POP()
    {
        if (i == stack_empty)
        {
            throw std::runtime_error{ "Stack is empty" };
        }

        T &amp;removed = arrays[i];
        i--;
        return removed;
    }
    T TOP()
    {
        if (i == stack_empty)
        {
            throw std::runtime_error{ "Stack is empty" };
        }

        return arrays[i];
    }

};


int main()
{
    Stack &lt;int&gt; Q;
    int option, push;
    do {
        std::cout &lt;&lt; "Select any of the following option" &lt;&lt; std::endl;
        std::cout &lt;&lt; "1.Push\n2.Pop\n3.Top\n4.Exit" &lt;&lt; std::endl;
        std::cin &gt;&gt; option;

        try {
            switch (option)
            {
            case 1:
                std::cout &lt;&lt; "Which element to push" &lt;&lt; std::endl;
                std::cin &gt;&gt; push;
                Q.PUSH(push);
                break;
            case 2:
            {
                int removed = Q.POP();
                std::cout &lt;&lt; removed &lt;&lt; " has been popped" &lt;&lt; std::endl;
                break;
            }
            case 3:
            {
                int top = Q.TOP();
                std::cout &lt;&lt; "Element at top of stack is " &lt;&lt; top &lt;&lt; std::endl;
                break;
            }
            case 4:
                break;
            default:
                std::cout &lt;&lt; "Error!" &lt;&lt; std::endl;
                break;
            }
        }
        catch (std::runtime_error error) {
            std::cout &lt;&lt; error.what() &lt;&lt; std::endl;
        }
    } while (option &lt; 4);
}