Check out example codes for "sum of stack c++". It will help you in understanding the concepts better.

Code Example 1

stack<int> si;
    int sum = 0;
    stack<int> tsi(si);
    while (!tsi.empty()) {
        sum += tsi.top();
        tsi.pop();
    }

Learn ReactJs, React Native from akashmittal.com