38.9k views
4 votes
Create a class template with this class code with any data type.

#include
using namespace std;
class Stack {
private:
int top;
int arr[4];
public:
Stack() {
top = -1;
}
bool push(int a);
int pop();
bool isEmpty();
};
bool Stack::push(int a) {
if (top >= 3) {
cout << "Stack is Full:";
return false;
}
else {
arr[++top] = a;
return true;
}
}
int Stack::pop() {
if (top < 0) {
cout << "Stack is Empty:";
return 0;
}
int a = arr[top--];
return a;
}
bool Stack::isEmpty() {
return(top < 0);
}
int main()
{
Stack s;
s.push(8);
s.push(1);
s.push(4);
cout << "\\Stack elements are: ";
while (!s.isEmpty()) {
cout << s.pop() << " ";
}
s.push(4);
s.push(8);
s.push(1);
cout << "\\Stack elements after arrangement are: ";
while (!s.isEmpty()) {
cout << s.pop() << " ";
}
return 0;
}

User Pixelmike
by
7.6k points

2 Answers

4 votes

Answer: 1

Explanation: Create a class template with this class code with any data type.

#include

using namespace std;

class Stack {

private:

int top;

int arr[4];

public:

Stack() {

top = -1;

}

bool push(int a);

User Vimukthi
by
7.7k points
6 votes

Final answer:

The code provided is an implementation of a stack using a class template in C++.

Step-by-step explanation:

The subject of this question is Computers and Technology. The code provided is an implementation of a stack using a class template in C++. The class template Stack has a private data member top that keeps track of the top index of the stack and an array arr to store the elements of the stack.

The class template provides member functions such as push() to add elements to the stack, pop() to remove elements from the stack, and isEmpty() to check if the stack is empty.

The main function demonstrates the usage of the class template by creating an instance of Stack, pushing elements onto the stack, and then popping elements from the stack to display them.

User Scott Carey
by
7.9k points