Template is a new concept which
enables us to define generic functions and classes and thus provide support for
generic
programming. Generic programming is an approach
where generic types are used as parameters.
Generic Function:
- They are a mechanism which makes it possible to use one function or class to handle many different data types.
- We can design a single class/function which operates on many data types, instead of having to create a separate class/function for each type.
- When applied with class they are called as class templates.
- When applied with function they are called as function templates.
By creating generic function, you
can define the nature of the algorithm, independent of any data. Once you have
done this the compiler automatically generates the correct code for the type of
data that is actually used when you execute the function.
A generic function is created using
the keyword template.
A template can be used to create a family of classes or
functions. For example, a class template for an array class
would enable us to create arrays of various data types such as int, float.
Similarly, we can define a template for a function add ( ),
that would help us to create various versions of add ( ) for adding data type
values.
0 comments:
Post a Comment