Template in
C ++
is a very efficient feature provided in C++. It allows you to write a single
code and use it for various inter-related functions or classes. The former is
termed as function templates and the
latter is termed as class template in C ++. The general
syntax for a template in C ++ is:
template <class dataType>
declaration; template is a reserved word for defining template in C ++ and class in the heading refers to any built-in or user-defined type. dataType is the name of the data type, user-defined or built-in and is referred to as a formal parameter to the template in C ++ in the same way as variables are passed as parameters to the functions. In the heading, the word class can also be replaced by typename.
Function Template
in C ++
Suppose you wrote a function in C ++ that compares two integers and
returns the smaller one. And now, if you want to compare two characters or two
strings, you have to write separate functions for them (function overloading).
In short, you can’t use the already written function for integers for all other
data types.
Template in C ++ allows you to write a single code and use
it for all data types. This is the purpose what template in C ++ is designed for.
The general syntax of a function template
in C ++ is:
template <class dataType>
function definition;
where dataType is the formal parameter to the templates which is used to
specify the data type of parameters to the function, return type of the
function and the data type of variables declared within the function.
Lets define a function template:
template <class dataType>
Type getLarger(Type p, Type q){
if (p > q)
return p;
else
return q;
}
This function receives two variables as parameters and
returns the larger one. dataType in the function definition will be
replaced by the data type of the parameters when the function is called.
A point should be noted here that the above function
template will work only for those data types for which the operator > is defined.
The statement
cout << getLarger(11, 13) <<
endl;
makes a call to the function template getLarger. As
11 and 13 are integers, the dataType will be
replaced by int and the resulting code would be compiled and executed.
Class Template in C ++
Similar to the function template, class template in C ++ is designed to use a single code for
many related classes. Take the example of a class List. This is a class that
stores a list (array) of a data type and performs various functions on that
list. If we transform this class into a class template, then we can use a
single code to declare List of any data type.
General syntax of a class template in C ++ is:
Class template in C ++ is generally termed as parameterized types because a specific class is generated on the basis of parameter type.
General syntax of a class template in C ++ is:
template <class dataType>
class declaration;Class template in C ++ is generally termed as parameterized types because a specific class is generated on the basis of parameter type.
C++ Programming Example
Header File and Implementation File of A Class Template
in C ++
Generally when we define a class, we place the definition of the class
in header file and the definition of the member functions of the class in
implementation file. The appropriate code for the object declared in main is
linked with the code of the user. However, this separation of class definition and
its member functions doesn’t work with class template in C ++. Passing
parameters to a function has an effect on run time but passing parameters to a
class template has an effect on compile time. As the parameters to a class template in C ++ are
defined in user code and the compiler cannot instantiate a class template
without parameters to the class, we cannot compile the implementation file
independent of the user code.
Learn C Online provides you various solutions for this problem of class template in C ++:
1) Put the class definition and the definition of the member
functions in the client code
2) Put the class definition and the definition of the member
functions in the same header file
3) Put the class definition and the definition of the member
functions in separate files (as discussed above) but include a directive to the
implementation file at the end of the header file
0 comments:
Post a Comment