Q:

Initialised arrays showing different values in a loop

I initialised an array and tried displaying the elements using loop and recursion but every time it shows different values than the original ones.
I tried displaying the elements individually and it works fine.
This is the class definition in which the array is defined:
class stack
{
    public:
        int top, *arr;
        stack(int s)
        {
            top=-1;
            size=s;
            arr=def_arr(s);
        }
        void push(int num)
        {
            if(top>=size-1)
            {
                cout<<"Stack has reached maximum length";
            }
            else
            {
                top++;
                arr[top]=num;
            }
        }
        int pop()
        {
            if(top>-1)
            {
                int temp;
                temp=arr[top];
                top--;
                return temp;
            } 
            else
            {
                cout<<"The stack has no values";
            }
        }
        void print()
        {
            if(top>-1)
            {
                for(int i=0; i<=top; i++)
                {
                    cout<<arr[i];
                    cout<<"\t";
                }
            }
            else
            {
                cout<<"Can\'t print stack of length 0";
            }
        }
    private:
        int size;
        int *def_arr(int size)
        {
            int arr[size];
            return arr;
        }
};

And the code that I ran:
int main()
{
    stack A(3);
    A.push(5);
    A.push(8);
    A.push(10);
    cout<<A.arr[1]<<"\n";
    A.print();
}

And the result:
8
5       87      -1259567440               

What am I missing?                        

A:

int *def_arr(int size)
    {
        int arr[size];
        return arr;
    }

is wrong because it returns a pointer to a local variable arr. Once the function exits the local variable no longer exists, so the pointer points at an invalid address (aka a dangling pointer). Using that pointer is undefined behaviour.
And a variable length array (VLA) is a non-standard construct in C++.
Two possible solutions
1) Use a std::vector
#include <vector>

std::vector<int> def_arr(int size)
    {
        std::vector<int> arr(size);
        return arr;
    }

This will require changes elsewhere in your code (mainly that arr in your class will have to be redeclared as a std::vector too).
2) Use dynamic memory allocation
int *def_arr(int size)
    {
        int* arr = new int[size];
        return arr;
    }

This will cause memory leaks in your code, fixing these is non-trivial, you should read up on the 'rule of three'.
Most people would recommend option 1. Congratulations you are about to learn about the most important topic in C++, resource management.

