C++ Program to implement Stack using template

By | 05.01.2017

Templated class from another templated class

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++ ?

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 :


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

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

        template <class T>
        class Stack
                void push(T i);
                T pop();
                int top;
        T st[100];

        template <class T>
                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;
                cout << int_stack.pop() << endl;
                cout << str_stack.pop() << endl;
                cout << str_stack.pop() << endl;
                return 0;


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


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.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.

Thanks for reading the post….

4 10 votes
Article Rating
Category: C++ Programming Template Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Notify of

Newest Most Voted
Inline Feedbacks
View all comments

Using template feature of C++, program a class ‘Calculator’ with two data
members of the user specified data type. The class should have a constructor to
initialize these data members and the following functions. (Both function
definitions are to be provided outside the class).
a. A function to add the two data members
b. A function to multiply the data members
From your main program create objects of calculator for integer and float data
members, call these functions and display the results.


#include <iostream> using namespace std;   template <class T> class Calculator { private:       T num1, num2;   public:       Calculator(T n1, T n2)       {              num1 = n1;              num2 = n2;       }         void displayResult()       {              cout << “Numbers are: “ << num1 << ” and “ << num2 << “.” << endl;              cout << “Addition is: “ << add() << endl;                            cout << “Product is: “ << multiply() << endl;              }         T add()       {              return num1 + num2;       }         T subtract()       {              return num1 – num2;       }         T multiply()       {              return num1 * num2;       }  … Read more »


Using the C++ template feature, code the following template based functions.
A function findAverage() which accepts an array of an arbitrary data type and
the number of elements in the array and returns the average of the elements in
the array.
A function minMax() which finds the minimum and maximum values in the array
as well as their indices. (You need to print all these values in the main() so you
may need call by reference).
In the main program, declare an array of 10 integers and 10 floats, call the above
functions and display their outputs in the main.


#include <iostream> using namespace std; template <class aqsa> aqsa findAverage(aqsa arr[], int size) {   aqsa sum = 0;   aqsa ave;   for (int i = 0; i < size; i++)   {     sum = arr[i] + sum;   }   ave = sum / size;   return ave; } template <class aqsa> void minMax(aqsa ar[], int size, int &min, int &max) {   for (int i = 0; i < size; i++)   {     if (ar[i] < min)     {       min = ar[i];     }     if (ar[i] > max)     {       max = ar[i];     }   } } int main() {   int a1[10] = { 2, 2, 2,  2,10,2, 2, 2, 2, 2,   };   int max = a1[0];   int… Read more »