You are on page 1of 9

Generic Programming with templates

November 18, 2022

Generic Programming with templates November 18, 2022 1/9


Outline

1 Need for Templates

2 Definition of class templates

3 Function Templates

4 Working of Function Templates

5 CLASS TEMPLATES WITH MORE PARAMETERS

6 FUNCTION TEMPLATES WITH MORE ARGUMENTS

Generic Programming with templates November 18, 2022 2/9


Need for Templates

Need for Templates


A template is a technique that allows a single function or class to
work with different data types.
The formal arguments of a template function are of template (generic)
type. They can accept data of any type, such as int, float, and long.
Usually, we overload a function when we need to handle different data
types. This approach increases the program size. The disadvantages
are that not only the program length is increased but also more local
variables are created in the memory.

Figure: Working with Non-Templates Figure: Working November


with Templates
Generic Programming with templates 18, 2022 3/9
Definition of class templates

Definition of Class Templates


Syntax to declare a class of template type:
template < class T>
class name_of_class
{
// class data member and function
}
The first statement template < classT > tells the compiler that the
following class declaration can use the template data type.
The T is a variable of template type that can be used in the class to
define a variable of template type.
The <>(angle bracket) is used to declare the variables of template
type that can be used inside the class to define the variables of
template type.
Templates cannot be declared inside classes or functions. They should
be global and should not be local. T k; where, k is the variable of
template type.
Generic Programming with templates November 18, 2022 4/9
Definition of class templates

Example

Write a program to show values of different data types using overloaded


constructor.
Write a program to show values of different data types using constructor
and template.

Generic Programming with templates November 18, 2022 5/9


Function Templates

Function Templates

//Normal Template Function Declaration


template < class T>
retuntype function_name (arguments )
{
// code
}
Write a program to define normal template function.
Write a program to create square() function using template.

Generic Programming with templates November 18, 2022 6/9


Working of Function Templates

Working of Function Templates

When the template function is called at that moment, from the type
of argument passed to the template function, the compiler identifies
the data type.
Then, every argument of template type is replaced with the identified
data type; this process is called instantiating.
Thus, according to different data types, respective versions of the
template function are created.
Though the template function is compatible for all data types, it will
not save any memory.

Generic Programming with templates November 18, 2022 7/9


CLASS TEMPLATES WITH MORE PARAMETERS

CLASS TEMPLATES WITH MORE PARAMETERS

template <class T1, class T2>


class name_of_ class
{
// class declarations and definitions
}
Write a program to define a constructor with multiple template variables.

Generic Programming with templates November 18, 2022 8/9


FUNCTION TEMPLATES WITH MORE ARGUMENTS

FUNCTION TEMPLATES WITH MORE ARGUMENTS

template <class A, class B, class C>


void show(A c,B i,C f)
{
cout<<"\n c="<<c <<" i="<<i <<"f="<<f;
}

Generic Programming with templates November 18, 2022 9/9

You might also like