Q:

C++ Program to implement Stack using template

belongs to collection: C++ Template Solved Programs

0

Write a C++ Program to implement push and pop methods from stack using template. Here’s a Simple C++ Program to implement Stack using template 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++ Program to implement Stack using template which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C++ Program to implement Stack using template  */

    #include <iostream>
        #include <string>
        using namespace std;

        template <class T>
        class Stack
        {
        public:
                Stack();
                void push(T i);
                T pop();
        private:
                int top;
        T st[100];
        };

        template <class T>
        Stack<T>::Stack()
        {
                top = -1;
        }

        template <class T>
        void Stack<T>::push(T i)
        {
                st[++top] = i;
        }

        template <class T>
        T Stack<T>::pop()
        {
                return st[top--];
        }

        int main ()
        {
                Stack<int> int_stack;
                Stack<string> str_stack;
                int_stack.push(67);
                str_stack.push("Hello");
                str_stack.push("Codezclub");
                cout << int_stack.pop() << endl;
                cout << str_stack.pop() << endl;
                cout << str_stack.pop() << endl;
                return 0;
        }

OUTPUT : :


/*  C++ Program to implement Stack using template  */

67
Codezclub
Hello

Process returned 0

Above is the source code and output for C++ Program to implement Stack using template 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 Add two numbers using function temp... >>
<< C++ Program to implement Generic methods on Stack ...