Lab 5 fl23733
Lab 5 fl23733
Lab 5 fl23733
Islamabad
#include <iostream>
using namespace std;
template <class type>
class input {
private:
type a;
public:
input();
void show();
};
template <class type>
input<type>::input() {
cout << "Enter a number: ";
cin >> a;
}
template <class type>
void input<type>::show()
{
cout << "The number you entered is: " << a << endl;
}
int main() {
input<int> Obj1;
Obj1.show();
input<float> Obj2;
Obj2.show();
return 0;
}
2. You are required to implement following operations of Stack using array in C++.
Push
Pop
Top
IsEmpty
IsFull
Your Stack class should have a constructor and destructor. The program should also
contain
the main function which calls the stack operations and display the values in stack
#include <iostream>
template<class ItemType>
class StackType {
public:
StackType(int size);
~StackType();
void Pop();
private:
int top;
int maxStack;
ItemType* items;
};
template<class ItemType>
StackType<ItemType>::StackType(int size) {
top = -1;
maxStack = size;
template<class ItemType>
StackType<ItemType>::~StackType() {
delete[] items;
template<class ItemType>
template<class ItemType>
template<class ItemType>
{
return items[top];
template<class ItemType>
top++;
items[top] = newItem;
template<class ItemType>
void StackType<ItemType>::Pop()
top--;
int main() {
StackType<int> stack(5);
int values[5];
if (!stack.IsFull()) {
stack.Push(values[i]);
}
}
cout << "Popping all values from the stack..." << endl;
while (!stack.IsEmpty()) {
stack.Pop();
if (!stack.IsEmpty()) {
cout << "New top after pop: " << stack.Top() << endl;
else {
return 0;