Templates in C
Templates in C
Templates
• A template is a mechanism that allows you to create
functions and classes that can be reused with every
data types.
• Templates are powerful features of C++ which allows
you to write generic programs. In simple terms, you can
create a single function or a class to work with different
data types using templates
• Templates are referred as generic types.
• Templates offers us the feature of generic
programming[It is an approach where generic types
are used as parameters in algorithms so that they work
for a variety of suitable data types and data structures].
• C++ provides two kinds of templates which can
exhibit generic programming, such as
– Class template(or Generic classes)
Templates continued…..
• A template can be considered as a kind of macro[i.e.
Expanded at compile time]
• The difference is, compiler does type checking before
template expansion. The idea is simple, source code
contains only function/class, but compiled code may
contain multiple copies of same function/class.
• Template is defined with a parameter that would be
replaced by a specific data type at the time of actual
use of class or function, the templates are sometimes
called as parameterized classes or functions.
Function templates
• Function Templates are used to write a generic
function that can be used for different data types.
• Function templates are also known as generic
functions
• A function template works in a similar to a
normal function, with one key difference.
• A single function template can work with different
data types at once but, a single normal function
can only work with one set of data types.
• Normally, if you need to perform identical
operations on more than one types of data, then
we need to create multiple functions with suitable
types, which is a tedious task for the
programmers, so the better approach is to create
a generic function(or function template), when
Function template-Syntax
• A function template starts with the keyword
template followed by template parameter/s
inside < > which is followed by function
declaration.
template <class T>
returntype function_name(arguments of type T )
{
// body of function with type T
………..
}
• In the above code, T is a template argument( or
generic type) that accepts different data types
(int, float….etc), and class is a keyword.
• You can also use keyword typename instead of
Example1-Function template
(WAP to calculate area of square using
function template)
#include<iostream>
using namespace std; Output:
template<class T> Square of 3=9
T square(T num)
Square of
{
5.6=31.36
return (num*num);
} Square of
int main() 123.456=15241.4
{
int num1=3;
cout<<"\n Square of "<<num1<<" =
"<<square<int>(num1);
float num2=5.6;
cout<<"\n Square of "<<num2<<" =
"<<square<float>(num2);
double num3=123.456;
cout<<"\n Square of "<<num3<<" =
"<<square<double>(num3);
Example 2-Function template
(WAP to display minimum of two
include<iostream> numbers)
cout<<"enter float
using namespace std; value";
template<class T> cin>>p>>q;
T min1(T a,T b) cout<<"\nmin of
integer"<<min1<int>(x,
{
y);
return(a<b?a:b);
cout<<"\nmin of floating
} value"<<min1<float>(p
int main() ,q);
{ return 0;
int x,y; }
float p,q; //Output
cout<<"enter integer Enter integer values 3 9
value"; Enter float values 1.2 3.4
cin>>x>>y; Min of integer values 3
Example 3-Function template
(WAP to swap the values of two variables
using function template)
#include<iostream> cout<<"\n Before swapping:";
using namespace std; cout<<"\n inum1="<<inum1<<" and
template<class T> inum2="<<inum2;
void swap1(T &a, T &b) cout<<"\n fnum1="<<fnum1<<" and
fnum2="<<fnum2;
{
cout<<"\n c1= "<<c1<<" and
T temp=a;
c2="<<c2;
a=b;
swap1<int>(inum1,inum2);
b=temp;
swap1<float>(fnum1,fnum2);
}
swap1<char>(c1,c2);
int main()
cout<<"\n\n After Swapping:";
{
cout<<"\n inum1="<<inum1<<" and
int inum1=4,inum2=5; inum2="<<inum2;
float cout<<"\n fnum1="<<fnum1<<" and
fnum1=4.8,fnum2=-5.3; fnum2="<<fnum2;
char c1='A',c2='a'; cout<<"\n c1= "<<c1<<" and
c2="<<c2;
Example 4-Function template
(WAP to read the values of array and add their
elements using function template)
#include<iostream>
int main()
using namespace std;
{
#define MAX 5
int Array[MAX];
template<class Type>
float Array1[MAX];
Type sum(Type A[])
cout<<"\n Enter the integer
{ array elements : ";
Type Total = 0; read<int>(Array);
for(int i=0;i<MAX;i++) cout<<"\n Sum of elements =
Total += A[i]; "<<sum<int>(Array);
return Total; cout<<"\n Enter the float array
} elements : ";
template<class Type> read<float>(Array1);
void read(Type A[]) cout<<"\n Sum of elements =
{ "<<sum<float>(Array1);
for (int i=0;i<MAX;i++) }
cin>>A[i];
Function template with
multiple parameters
We can use more than one generic type in the
template function by using comma to separate the list.
Syntax:
template<class T1, class T2,.....>
return_type function_name (arguments of type T1,
T2....)
{
// body of function.
}
Example-Function template with
multiple parameters
#include<iostream> multiply<int,int>(2,2);
using namespace std; float num3=7.8;
template<class T1,class T2>
int num4=3;
void multiply(T1 num1,T2
num2) cout<<"\n Product of
{ num3*num4=";
cout<<num1*num2; multiply<float,int>(num3,n
} um4);
int main() cout<<endl;
{
multiply<float,float>(2.3,4.
int num1=3;
5);
float num2=4.5;
cout<<"\n Product of
return 0;
num1*num2="; }
multiply<int,float>(num1,nu
m2);
cout<<endl;
Overloading of function
templates
• A template function may be overloaded
either by a template function or ordinary
function of its name.
• Overloading resolution is accomplished as:
Compiler will first look to call an
ordinary function(or non-template
function) that has an exact match.
If ordinary function with exact match is
not present, then compiler will look to
call a template function that could be
created with an exact match.
An error is generated if no match is
found(either non-template or template).
Example-Overloading of function
#include<iostream>
templates
using namespace std;
template<class T>
void display(T x) //Function template with one generic type
{
cout<<"Template display:"<<x<<"\n";
}
template<class T1,class T2,class T3>
void display(T1 x,T2 y,T3 z) //Function template with three
generic types
{
cout<<"Template display:"<<x<<" "<<y<<"
"<<z<<"\n";
}
void display(int x)//Non-template function accepting one
integer argument
{
Example continued..
void display(char y)//Non-template function accepting one
character argument
{
cout<<"Explicit display:"<<y<<"\n";
}
int main()
{
display(100); //Calls display(non-template) which accepts one
integer argument
display(56.78);//Calls display(template) accepting one generic
type
display('a');// Calls display(non-template) which accepts one
character argument
display(5.98,'b',89);// Calls display(template) accepting three
generic types
return 0;
}
//Output
Class templates
• Like function templates, we can also
create class templates for generic class
operations
• Class templates are also known as generic
classes
• Sometimes, we need a class
implementation that is same for all
classes, only the data types used are
different.
• Normally, we would need to create a
different class for each data type /or
create different member variables and
functions within a single class.
Syntax of class template
template<class T>
class classname
{
//……………
//Class member specification with
//anonymous type T
//wherever appropriate.
……………
};