Q:

C++ Menu Driven Program for Stack using Templates

belongs to collection: C++ Template Solved Programs

0

Write a C++ Menu Driven Program for Stack using Templates. Here’s a Simple C++ Menu Driven Program for Stack using Templates in C++ Programming Language.


What are Templates in C++ ?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

 
 

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..


Function Template :

The general form of a template function definition is shown here:

 

template <class type> ret-type func-name(parameter list)

{
// body of function
}


Class Template :

Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:

template <class type> class class-name

{
.
.
.
}


Below is the source code for C++ Menu Driven Program for Stack using Templates which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* C++ Menu Driven Program for Stack using Templates  */

#include<iostream>
using namespace std;
template<class T>
class Stack
{
        public:
                Stack(int MaxStackSize);
                ~Stack(){delete[] S;}
                int IsEmpty()const{return top==-1;}
                int IsFull()const{return top==MaxTop;}
                T Peek()const;
                void Push(T);
                T Pop();
                void Display();
        private:
                int top; //current top of stack
                int MaxTop; //max val for top
                T *S; //element array
};
        template<class T>
Stack<T>::Stack(int MaxStackSize)
{
        //stack constructor
        MaxTop=MaxStackSize-1;
        S=new T[MaxStackSize];
        top=-1;
}
template<class T>
T Stack<T>::Peek()const
{
        if(IsEmpty()) //top fails
                return 0;
        else
                return S[top];
}
        template<class T>
void Stack<T>::Push(T x)
{
        if(IsFull())
                cout<<"\nno memory()"; //add fails
        else
        {
                S[++top]=x;
        }
}
        template<class T>
T Stack<T>::Pop()
{
        T x;
        if(IsEmpty())
        {
                cout<<"\nstack is empty\n";
                return -1;
        }
        else
        {
                x=S[top--];
                return x;
        }
}
        template<class T>
void Stack<T>::Display()
{
        if(IsEmpty())
                cout<<"\nout of bounds"; //delete fails
        else
                for(int i=top;i>=0;i--)
                {
                        cout<<S[i]<<"\t";
                }
}
void menu()
{
        cout<<"\n1.Push\n 2.Pop\n 3.Peek\n 4.Display\n";
}
int main()
{
        Stack<int>iobj(5);
        int ch,x;

        do
        {
                menu();
                cout<<"\nenter the choice\n";
                cin>>ch;
                switch(ch)
                {
                        case 1:
                                cout<<"\nenter x value to push into the stack\n";
                                cin>>x;
                                iobj.Push(x);
                                break;
                        case 2:
                                x=iobj.Pop();
                                if(x!=-1)
                                        cout<<"\npoped value is \t"<<x<<endl;
                                break;
                        case 3:
                                x=iobj.Peek();
                                cout<<"\ntop most value is \t"<<x<<endl;
                                break;
                        case 4:
                                iobj.Display();
                                break;
                }
        }while(ch>=1&&ch<=4);
return 0;
}

OUTPUT : :


/* C++ Menu Driven Program for Stack using Templates  */

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
1

enter x value to push into the stack
1

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
1

enter x value to push into the stack
2

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
1

enter x value to push into the stack
3

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
1

enter x value to push into the stack
4

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
1

enter x value to push into the stack
5

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
3

top most value is       5

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
4
5       4       3       2       1
1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
2

poped value is  5

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
2

poped value is  4

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
2

poped value is  3

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
2

poped value is  2

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
2

poped value is  1

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
2

stack is empty

1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
4

out of bounds
1.Push
 2.Pop
 3.Peek
 4.Display

enter the choice
5

Process returned 0

Above is the source code and output for C++ Menu Driven Program for Stack using Templates which is successfully compiled and run on Windows System to produce desired output.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ program to implement Hash Table using Template... >>
<< C++ Program of templated class derived from anothe...