Templates in C++
-> Templates is one of the features added to C++ Recently. it is a new concept which enable us to define Generic classes and functions and thus provides support for Generic programming.
-> Generic programming 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.
A Template can be used to create a family of classes or function.
for example: A class Template for an Array class would enable us to create arrays of various data types such as int Array and float Array.
-> A Template can be considered as a kind of macro.
-> The Templates are sometimes called parameterized classes or functions.
1. Function Templates:-
we can define a template for a function, say mul(), that would help us create various versions of mul() for multiplying int, float and double data type values.
we can define function Templates that could be used to create a family of functions with different Argument types.
-> The general format of a function Templates as follows:
template <class T>
returntype functionname (arguments of type T)
{
Body of function
with type T
::::
}
// C++ program to demonstrate function Templates.
#include<iostream>
using namespace std;
template <class T>
void swap1(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b)
{
cout<<"m and n before swap:";
cout<<m<<" "<<n<<"\n";
swap1(m,n);
cout<<"m and n after swap:";
cout<<m<<" "<<n<<"\n";
cout<<"a and b before swap:";
cout<<a<<" "<<b<<"\n";
swap1(a,b);
cout<<"a and b after swap:";
cout<<a<<" "<<b<<"\n";
}
int main()
{
fun(100,200,11.24,33.66);
return 0;
}
OUTPUT:
m and n before swap:100 200
m and n after swap:200 100
a and b before swap:11.24 33.66
a and b after swap:33.66 11.24
2. Class Templates:-
-> The class template definition is very similar to an ordinary class defintion except the prefix template <class T> and the use of type T.
-> This prefix tells the compiler that we are going to declare a template and use T as a type name in the declaration.
// C++ program to demonstrate class template.
#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 display()
{
cout<<"number 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;
}
T add()
{
return num1+num2;
}
T subtract()
{
return num1-num2;
}
T multiply()
{
return num1*num2;
}
T divide()
{
return num1/num2;
}
};
int main()
{
calculator<int>intcalc(2,1);
calculator<float>floatcalc(2.4,1.2);
cout<<"Int results:"<<endl;
intcalc.display();
cout<<endl<<"float results:"<<endl;
floatcalc.display();
return 0;
}
OUTPUT:
Int results:
number are:2and1
Addition is:3
subtraction is:1
product is:2
Division is:2
float results:
number are:2.4and1.2
Addition is:3.6
subtraction is:1.2
product is:2.88
Division is:2
-> Templates is one of the features added to C++ Recently. it is a new concept which enable us to define Generic classes and functions and thus provides support for Generic programming.
-> Generic programming 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.
A Template can be used to create a family of classes or function.
for example: A class Template for an Array class would enable us to create arrays of various data types such as int Array and float Array.
-> A Template can be considered as a kind of macro.
-> The Templates are sometimes called parameterized classes or functions.
1. Function Templates:-
we can define a template for a function, say mul(), that would help us create various versions of mul() for multiplying int, float and double data type values.
we can define function Templates that could be used to create a family of functions with different Argument types.
-> The general format of a function Templates as follows:
template <class T>
returntype functionname (arguments of type T)
{
Body of function
with type T
::::
}
// C++ program to demonstrate function Templates.
#include<iostream>
using namespace std;
template <class T>
void swap1(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b)
{
cout<<"m and n before swap:";
cout<<m<<" "<<n<<"\n";
swap1(m,n);
cout<<"m and n after swap:";
cout<<m<<" "<<n<<"\n";
cout<<"a and b before swap:";
cout<<a<<" "<<b<<"\n";
swap1(a,b);
cout<<"a and b after swap:";
cout<<a<<" "<<b<<"\n";
}
int main()
{
fun(100,200,11.24,33.66);
return 0;
}
OUTPUT:
m and n before swap:100 200
m and n after swap:200 100
a and b before swap:11.24 33.66
a and b after swap:33.66 11.24
2. Class Templates:-
-> The class template definition is very similar to an ordinary class defintion except the prefix template <class T> and the use of type T.
-> This prefix tells the compiler that we are going to declare a template and use T as a type name in the declaration.
// C++ program to demonstrate class template.
#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 display()
{
cout<<"number 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;
}
T add()
{
return num1+num2;
}
T subtract()
{
return num1-num2;
}
T multiply()
{
return num1*num2;
}
T divide()
{
return num1/num2;
}
};
int main()
{
calculator<int>intcalc(2,1);
calculator<float>floatcalc(2.4,1.2);
cout<<"Int results:"<<endl;
intcalc.display();
cout<<endl<<"float results:"<<endl;
floatcalc.display();
return 0;
}
OUTPUT:
Int results:
number are:2and1
Addition is:3
subtraction is:1
product is:2
Division is:2
float results:
number are:2.4and1.2
Addition is:3.6
subtraction is:1.2
product is:2.88
Division is:2