title: Implementation of stack using vectors in c++
body: <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  using namespace std;
  class Stack {
  
  private:
      int maxSize;
      vector&lt;int&gt; v;
      int top;
  public:
      Stack(int size) {
          this-&gt;maxSize = size;
          this-&gt;v.reserve(this-&gt;maxSize);
          this-&gt;top = -1;
      }
  
      void push(int j) {
          if (!(this-&gt;isFull())) {
              this-&gt;v[++this-&gt;top] = j;
          } else {
              cout &lt;&lt; "stack is full"&lt;&lt;endl;
          }
  
      }
  
      int pop() {
          if (!(this-&gt;isEmpty())) {
  
              return this-&gt;v[this-&gt;top--];
          } else {
              cout &lt;&lt; "\nstack is empty"&lt;&lt;endl;
              cout&lt;&lt;  "StackOverflow "&lt;&lt;endl;
          }
      }
  
      int peak() {
          return this-&gt;v[this-&gt;top];
      }
      bool isEmpty() {
          return (this-&gt;top == -1);
      }
  
      bool isFull() {
          return (this-&gt;top == this-&gt;maxSize - 1);
      }
  
  };
  
  int main() {
  
      Stack s(10);
  
      s.push(10);
      s.push(20);
      cout&lt;&lt;s.pop();
      cout&lt;&lt;"\n"&lt;&lt;s.pop();
      s.push(40);
      cout&lt;&lt;"\n"&lt;&lt;s.pop();
  
  }
  </code></pre>
  
  <p>How can I make this code more better and reliable for these reasons:</p>
  
  <ul>
  <li><p>The output of this code is 20 10 40 .</p></li>
  <li><p>But in the output I want to print <strong>"Stack is empty"</strong> after every
  time the stack is empty after popping out all the elements from the
  stack</p></li>
  <li><p>It fails toprint <strong>"Stackis Empty "</strong> every time .</p></li>
  </ul>