1 of 19

Templates

2 of 19

Need for Templates

  • Consider the following function for add 2 integers:

Void sum(int x,int y)

{

int z;

z=x+y;

cout<<z;

}

3 of 19

  • When different data types like float or double are required to be added, we have to change the data types in the program.

  • A program usually rewrites the functions with appropriate data types.

  • This causes (sometimes) compile time errors.

  • Another method of handle this problem is function overloading.

4 of 19

  • But ,when we define a function template, we code only one function with generic types and use the same function with required types as and when needed.

  • This is the major advantage of Templates.

5 of 19

Templates

  • Used to define generic classes and functions.
  • Generic Programming
  • An Approach where generic types are used as parameters so that they work for suitable data types and data structure.
  • A template can be used to create a family of classes and functions.

6 of 19

Example

  • A class template for an array class would enable us to create array of various data types such as int array or float array.

  • Similarly, we can define function template. Say mul() function that would help us to create various versions of mul() for multiply int,float, double type values.

7 of 19

Function Template

  • To create function template, we have to declare the function in the template and declaring a generic data type such as T.

  • Generic Format/Syntax :

template <class T>

return_type fname(argu of type T)

{

}

8 of 19

Example

template <class T>

void sum(T x, T y)

{

T z;

z=x+y;

cout<<z;

}

int main()

{

sum(5,10);

sum(6.5,7.5);

return 0;

}

  • In this, we defined a generic type T.

  • Called Template Argument.

  • If there is some return type:

Then

T sum(T x, T y)

9 of 19

Parameterized classes & functions

  • Templates are sometimes called Parameterized classes and functions.

  • Because template is defined with parameter that would be replaced by a specified data type at the time of actual use of class or function.

10 of 19

Function Template with multiple Parameters

template <class T1,class T2>

Void display(T1 x, T2 y)

{

cout<<x;

cout<<y;

}

int main()

{

display(8.25,’m’);

display(789,65.43);

return 0;

}

11 of 19

Class Template

Template<class T>

Class class_name

{

//With type where //appropriate

}

Template <class T>

Class sample

{

T x;

T y;

Public:

getdata(T a, T b)

{

x=a;

y=b;

}

12 of 19

display()

{

cout<<x;

cout<<y;

}

Void Main()

{

Sample<int> obj;

obj.getdata(4,6);

obj.display();

Sample <float> obj1;

Obj1.getdata(3.5,6.5);

Obj1.display();

}

13 of 19

Member function TemplateWe can define a member function outside the class, we have to declare the template before every member function.

Template <class T>

class_name<T>::fun_name(argu)

{

}

Template <class T>

void sample<T>::getdata(T a,T b)

{

x=a;

y=b;

}

14 of 19

Overloading of Template function

  • A template function may be overloaded either by template functions or ordinary functions of its name.

  • In such cases, overloading resolution is accomplished as follows:

1. call an ordinary function that has an exact match

2. call a template function that could be created with an exact match.

3. An error is generated if no match is found.

  • No automatic conversions are applied to arguments on the template functions.

15 of 19

Example

Template<class T>

Void display(T x)

{

cout<<“template display”

}

Void display(int x)

{

cout<<“explicit display”;

}

Void Main()

{

display(100);

display(3.4);

display(‘C’);

}

16 of 19

Non-type template arguments

  • Template can have multiple arguments.

  • It is also possible to use non-type template arguments.

  • In addition to type argument T, we can also use other arguments such as int, char, float, string etc.

17 of 19

Example

template<class T,int size>

class array

{

public:

T a[size];

void getdata()

{

cout<<"enter elements";

for(int i=0;i<size;i++)

{

cin>>a[i];

}

}

void display()

{

cout<<"elements are:";

for(int i=0;i<size;i++)

{

cout<<a[i];

}

}};

int main()

{

array<int,10> a1;

// array of 10 integers

array<float,5>a2;

// array of 5 floats

array<char,20>a3;

// string of size 20

a1.getdata();

a1.display();

return 0;

}

18 of 19

Write a function template for finding the maximum value in array.

19 of 19

Template<class T>

Void Max(t a[],int n)

{

int i;

T max=0;

for(i=0;i<n-1;i++)

{

If(a[i]>max)

{

max=a[i];

}

}

cout<<max;

}

Void main()

{

int x[5]={1,55,33,2,4};

Max(x,5);

}