stack implementation using class in c++

Stack Implementation Using Class in C++

#include <iostream>
using namespace std;

#define MAX_SIZE 100

class Stack {
  int top;
public:
  int arr[MAX_SIZE];

  Stack() { top = -1; }
  bool push(int x);
  int pop();
  int peek();
  bool isEmpty();
};

bool Stack::push(int x) {
  if (top >= (MAX_SIZE - 1)) {
    cout << "Stack Overflow";
    return false;
  }
  else {
    arr[++top] = x;
    cout << x << " pushed into stack\n";
    return true;
  }
}

int Stack::pop() {
  if (top < 0) {
    cout << "Stack Underflow";
    return 0;
  }
  else {
    int x = arr[top--];
    return x;
  }
}

int Stack::peek() {
  if (top < 0) {
    cout << "Stack is Empty";
    return 0;
  }
  else {
    int x = arr[top];
    return x;
  }
}

bool Stack::isEmpty() {
  return (top < 0);
}

int main() {
  Stack stack;
  stack.push(10);
  stack.push(20);
  stack.push(30);
  cout << stack.pop() << " popped from stack\n";
  cout << "Top element is " << stack.peek() << "\n";
  cout << "Is stack empty? " << (stack.isEmpty() ? "Yes" : "No") << "\n";
  return 0;
}

This code implements a stack using a class in C++. The class Stack has private member top and public member arr of integers with a constant MAX_SIZE set to 100. The class has methods push, pop, peek, and isEmpty for stack operations. The main function demonstrates the usage of the stack by pushing, popping, peeking, and checking if the stack is empty.