DEV Community

Cover image for Templates in C++
Abhishek Chandra
Abhishek Chandra

Posted on • Updated on

Templates in C++

Templates in C++ Language

The keyword template is used to define function templates and class templates in our C++ program.
It introduces generic programming, which makes a way for programmers to write more efficient code.

So, now there are two ways in which we can use template,

  • Function template

    Function template is also known as generic function.

syntax creating function template:

template<class type> 
type func_name(type arg1, type arg2, ....){
     ...
};
// here type is a placeholder for generalisation of data type.
Enter fullscreen mode Exit fullscreen mode

It creates a function template named func_name with any number of arguments.

Let's learn this with an example,

Program to find the smaller number from the given two numbers* without the use of template*:

#include<iostream>

using namespace std;

// without the use of template keyword.
int small(int a, int b){
    if(a > b){
        return(b);    
    }else{
        return(a);
    }
};

double small(double a, double b){ // function overloading
    if(a > b){
        return(b);
    }else{
        return(a);
    }
};


int main(){
    cout<<small(2,6)<<endl;
    cout<<small(2.4,1.9);
    return 0;
}

/*
OUTPUT
2
1.9
*/
Enter fullscreen mode Exit fullscreen mode

(32 Lines of Code)

To avoid using different data types in a function, Use template.

#include<iostream>

using namespace std;

// Program using template keyword
template<class X>
X small(X a, X b){ // here X is a generic data type, making 'small' a generic function.
    if(a < b){
        return(b);
    }else{
        return(a);
    }
};

int main(){
    cout<<small(3,4)<<endl;
    cout<<small(3.4,1.6); // no function overloading reduces the lines of code in a program
    return 0;
}

/*
OUTPUT
3
1.6
*/

Enter fullscreen mode Exit fullscreen mode

(25 Lines of Code)

Example 2:

template<class X, class Y>  
X big(X a, Y b){        //different data types
    if(a > b){
        return a;
    }else{
        return b;
    }
};

int main(){
    cout<<big(4,3.4)<<endl;
    cout<<big(1.99,-5);
    return 0;
}

/*
OUTPUT:
4
1.99
*/

Enter fullscreen mode Exit fullscreen mode

(20 Lines of Code)

  • Class template

    Class template is also known as generic class.

template<class type> 
class class_name{
    ...
};
// here type is also a placeholder to generalise a class.
Enter fullscreen mode Exit fullscreen mode

It creates a class template named class_name.

Let's also learn this with an example,
Here we are making a arrayList class without the use of template.

#include<iostream>

using namespace std;

class arrayList{
    private:
        struct controlBlock{
            int capacity;
            int *arr_ptr;
        };

    controlBlock *s;

    public:
        arrayList(int capacity){ // constructor of arrayList class
            s = new controlBlock;
            s -> capacity = capacity;
            s -> arr_ptr = new int[s->capacity];
        }
        void addElement(int index, int data){ // method to add elements to the list
            if(index >= 0 && index <= s->capacity - 1){
                s -> arr_ptr[index]  = data;
            }else{
                cout<<"Array index not valid";
            }
        }
        void viewElement(int index, int &data){
            if(index >=0 && index <= s->capacity - 1){
                data = s->arr_ptr[index];
            }else{
                cout<<"Array index not valid";
            }
        }
        void viewList(){ // method to view the list
            int i;
            for(i = 0; i < s->capacity; i++){
                cout<<" "<<s->arr_ptr[i];
            }
        }
};

int main(){
    int data;
    arrayList list1(4);
    list1.addElement(0,2);
    list1.addElement(1,12);
    list1.addElement(2,22);
    // list1.addElement(3,32); if not assigned, by default 0 is assigned.
    // list1.viewElement(0,data);
    // cout<<data;
    list1.viewList();
    return 0;
}

/*
OUTPUT:
2
12
22
0
*/
Enter fullscreen mode Exit fullscreen mode

(61 Lines of Code)

Image explanation of code.

template_c++.png

Now, templating our above program will result in increased possibilities of input with the same number of lines of code.

visit : arrayList_with_template.cpp

Go read about STL in this article.

Top comments (0)