Lecture 27 Templates
Lecture 27 Templates
Templates
By :
Dr. Rinkle Rani
Associate Professor, CSED
TIET, Patiala
1
Need of template
• Example Program- To find the greater of two numbers.
2
Need of template - motivation
• To reduce code duplication when supporting numerous data types
• Template make your function or class generalize as far as data type is
concerned.
3
We can write a generic code for We can write a generic code for a
a function e.g. add() for class, to manipulate group of
integers, double, float etc. member variables & functions e.g.
linked list of strings, integers etc.
4
Function Templates(Generics) -
definition
• We write a generic function that can be used for different data types. Syntax of an example
function -
10
Answer:
template <class T1, class T2>
void show(T1 a, T2 b){
cout<<a<<“, “<<b<<endl;
}
11
Class Templates
12
Class templates
• Class templates are used when a class uses logic that can be generalized.
• The compiler will automatically generate the correct type of object, based
upon the type you specify when object is created.
• Syntax:
template <class Placeholder> class class-name
{
…….
}
• Once you created class template, object can be generated
using the following form:
Class-name<datatype> ob; 13
// Template class example
template <class T> int main(){
class Test { Test <int> t1(50,60);
T a; Test <double> t2(-10.20,
30.4);
T b;
Test <char> t3(‘a’, ‘c’)
public: t1.show();
Test (T x, T y) {a=x; b=y; } t2.show();
void show() t3.show();
{cout<<a<<endl; }
cout<<b<<endl;}
};
14
Defining function outside the template class
template <class T>
class Test {
T a;
T b;
public:
Test (T x, T y) {a=x; b=y; }
void show();
};
template <class T>
void Test<T> :: show()
{cout<<a<<endl;
cout<<b<<endl;}
} 15
Class template with multiple parameters
#include <iostream>
using namespace std; int main()
template <class X, class Y> {
class myclass
myclass<int, double> ob1(10, 0.23);
{
X i; myclass<char, int> ob2('X', 30);
Y j; ob1.show(); // show int, double
public: ob2.show(); // show char, int
myclass(X a, Y b) { i = a; j = b; }
return 0;
void show() { cout << i << ' ' << j << '\
n'; } }
}; 16
Template Specialization
in C++
17
Template Specialization
• What if we want a different code for a particular data type?
• Consider a big project that needs a function sort() for arrays of many
different data types.
• Let Quick Sort be used for all data types except char.
• In case of char, total possible values are 256 and counting sort may be
a better option.
• Is it possible to use different code only when sort() is called for char
data type?
18
• Solution: Template Specialization
• In C++, if you want to get a special behavior for a particular data type.
This is called template specialization.
19
Example
#include <iostream> int main()
using namespace std;
{
template <class T>
fun<char>('a');
void fun(T a)
{ fun<int>(10);
cout << "The main template fun(): " << a << endl; fun<float>(10.14);
} }
template<>
void fun(int x)
{
cout << "Specialized Template for int type: "
<< x << endl;
} 20
Output
• The main template fun(): a
• Specialized Template for int type: 10
• The main template fun(): 10.14
21