Simple Stack Program Example Using Class in C++
On this page (6sections)
About this program
This is an example program in stack programs. Read the concept first: Understanding Array in C++, then study the code and output below.
Definition
A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.
Simple Stack Example Program Using Class
/* Simple Stack Program Example Using Class in C++*/
/* Data Structure Programs,Stack Programs,C++ Examples */
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
class Stack {
private:
int item, i;
int arr_stack[MAX_SIZE];
int top;
public:
Stack() {
top = 0;
}
void push() {
if (top == MAX_SIZE)
cout << "\n## Stack is Full!";
else {
cout << "\nEnter The Value to be pushed : ";
cin>>item;
cout << "\n## Position : " << top << ", Pushed Value :" << item;
arr_stack[top++] = item;
}
}
void pop() {
if (top == 0)
cout << "\n## Stack is Empty!";
else {
--top;
cout << "\n## Position : " << top << ", Popped Value :" << arr_stack[top];
}
}
void display() {
cout << "\n## Stack Size : " << top;
for (i = (top - 1); i >= 0; i--)
cout << "\n## Position : " << i << ", Value :" << arr_stack[i];
}
};
int main() {
int choice, exit_p = 1;
Stack obj;
cout << "\nSimple Stack Example - Class and Member Functions - C++";
do {
cout << "\n\nStack Main Menu";
cout << "\n1.Push \n2.Pop \n3.Display \nOthers to exit";
cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
obj.push();
break;
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);
return 0;
}
Sample Output
Simple Stack Example - Class and Member Functions - C++
Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 5
## Position : 0, Pushed Value :5
Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 0, Popped Value :5
Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 3
## Stack Size : 0
Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 4
Related Pages
Learn the concept first, then study the code:
- Data Structures — Browse all Data Structures.
- Understanding Array in C++ — Tutorial — stack implementations using arrays or classes.
- Simple Stack Program in C++ Programming — More in stack programs.
- Simple Stack Program using functions in C++ Programming — More in stack programs.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Simple Stack Program Example Using Class, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, conditional logic and reading user input, illustrating a common pattern in C++ programming.