Classes-Template in C++
Classes-Template in C++
A template is a simple and yet very powerful tool in C++. The simple idea is to
pass data type as a parameter so that we don’t need to write the same code for
different data types. For example, a software company may need sort() for
different data types. Rather than writing and maintaining the multiple codes, we
can write one sort() and pass data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’.
The second keyword can always be replaced by keyword ‘class’.
Function Templates We write a generic function that can be used for different
data types. Examples of function templates are sort(), max(), min(),
printArray().
Know more on Generics in C++
C++ Templates
A C++ template is a powerful feature added to C++. It allows you to define the generic classes
and generic functions and thus provides support for generic programming. Generic programming
is a technique where generic types are used as parameters in algorithms so that they can work for
a variety of data types.
o Function templates
o Class templates
Function Templates:
We can define a template for a function. For example, if we have an add() function, we
can create versions of the add function for adding the int, float or double type values.
Class Template:
We can define a template for a class. For example, a class template can be created for
the array class that can accept the array of various types such as int array, float array or
double array.
Function Template
o Generic functions use the concept of a function template. Generic functions define a set
of operations that can be applied to the various types of data.
o The type of the data that the function will operate on depends on the type of the data
passed as a parameter.
o For example, Quick sorting algorithm is implemented using a generic function, it can be
implemented to an array of integers or array of floats.
o A Generic function is created by using the keyword template. The template defines what
function will do.
Where Ttype: It is a placeholder name for a data type used by the function. It is used within the
function definition. It is only a placeholder that the compiler will automatically replace this
placeholder with the actual data type.
In the above syntax, we have seen that the template function can accept any number of
arguments of a different type.
In the above example, we use two generic types in the template function, i.e., X and Y.
Overloading a Function Template
We can overload the generic function means that the overloaded template functions
can differ in the parameter list.
CLASS TEMPLATE
Class Template can also be defined similarly to the Function Template. When a class uses the
concept of Template, then the class is known as generic class.
Syntax
Ttype is a placeholder name which will be determined when the class is instantiated. We
can define more than one generic data type using a comma-separated list. The Ttype
can be used inside the class body.
Now, we create an instance of a class
1. class_name<type> ob;
type: It is the type of the data that the class is operating on.
Syntax
Let's see a simple example when class template contains two generic data types.
Let's see a simple example of nontype template arguments.
In the above example, the class template is created which contains the nontype
template argument, i.e., size. It is specified when the object of class 'A' is created.
Conclusion
Points to Remember