Home > OOP > Template Classes & Functions

Templates laid the foundation of generic programming. i.e. writing you code in a way that is independent of a specific data type. They are a blueprint for creating generic programs.
Why use templates?
Answer to this question is in another question. i.e. what is better than having a number of classes that will perform same task or do same thing to different data types?
One class is much better that allows user to choose required data type and perform task according to it.
In C++ we can create Templates for classes and Functions.

C++ Template Function

We can make template for any function which we want to use with more than one data types. i.e. if we want to add two numbers we can make a function template for this function because two numbers can be integers, float or double. Instead of writing different function for each data type we will make a function template.
Syntax to make a function template is:

template   type function-name (argument/s) 
{
    // body of function
}

As we are making it generic so if we want to return any value from function it must be of generic type same is the case with arguments.
template is reserved word used to create a template.
class is also a reserved word.
type is the generic name used for template we created.

Example program: –

#include "stdafx.h"
#include "iostream"
using namespace std;
class A
{
    public:
    template general add(general &n1, general &n2) 
    {
        return n1 + n2; 
    } 
}; 

int main() 

{ 
    int n1 = 10, n2 = 20; 
    float n3 = 15.3, n4 = 13.4; 
    char n5 = 'a', n6 = 'c'; 
    A objA; 
    cout << "Adding integers in template Function : " << objA.add(n1, n2); 
    cout << "\nAdding floating points in template Function : " << objA.add(n3, n4); 
    cout << "\nAdding characters in template Function : " << objA.add(n5, n6)<<endl; 
    return 0; 
}

Output: –

C++ Template function

 

C++ Template Class

Just like functions we can also define template classes. These classes can also be used with different data types. We make template classes for the same purpose as we have for template functions. i.e. not to define separate classes for each data type.
Generic syntax to create a template class is:

template   class class-name 
{
     // body of class 
}

Example Program: –

#include "stdafx.h"
#include "iostream" 
using namespace std;
template  class calculator 
{ 
    general num1, num2; 
    public: 
    calculator(general n1, general n2) 
    { 
        num1 = n1; 
        num2 = n2;
    } 
    void displayResult() 
    { 
        cout << "Numbers are: " << num1 << " and " << num2 << "." << endl; 
        cout << "Addition is: " << add() << endl; 
        cout << "Subtraction is: " << subtract() << endl; 
        cout << "Product is: " << multiply() << endl; 
        cout << "Division is: " << divide() << endl; 
    } 
    general add() 
    { 
        return num1 + num2; 
    } 
    general subtract() 
    { 
        return num1 - num2; 
    } 
    general multiply() 
    { 
        return num1 * num2; 
    } 
    general divide() 
    { 
        return num1 / num2; 
    } 
};
int main() 
{ 
    cout << "*****For Integer Data type*****"<<endl; 
    calculator c(10, 20);
    c.displayResult();
    cout << "\n*****For Float Data type*****" << endl; 
    calculator c1(13.5, 28.3);
    c1.displayResult(); 
    return 0;
}

Output:-

C++ Template class

Leave a Reply

Your email address will not be published. Required fields are marked *