Template
A template is a simple and most powerful tool in C++. It is a frame
which is used to pass the data type as a parameter so that we do not need
to write the same code for different data types.
For example, a programmer may need to sort() elements for different
data types. Rather writing and maintaining multiple codes, programmer
can write one sort() function and pass the datatype as a parameter.
It uses keyword like ‘template’ and ‘type_name’ or ‘class’.
Types of templates:
1. Function Templates (Generic Function- Template for Function)
2. Class Templates (Generic Class – Template for class)
How templates works
Templates are expanded at compile time as macros. The difference is,
the compiler does type-checking before template expansion.
The source code contains only function/ class.
But the compiled code may contain multiple copies of the same
function/class.
Function Templates
A function templates or generic function can be used for different data
types.
Syntax:
template <class/ typename Ttype> return_type function_name
(parameter_list)
{
//body of function
}
, class : It is a keyword is used to specify a generic type in a template
declaration.
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 with the actual data type.
Example 1
#include<iostream>
using namespace std;
template<typename T>T mymax(T x, T y)
{
return (x>y)?x:y;
}
main()
{
cout<<mymax<int>(3,7)<<"\n";
cout<<mymax<double>(3.0,7.0)<<"\n";
cout<<mymax<char>('g','e');
}
Output
7
7
g
A template is a simple and most powerful tool in C++. It is a frame
which is used to pass the data type as a parameter so that we do not need
to write the same code for different data types.
For example, a programmer may need to sort() elements for different
data types. Rather writing and maintaining multiple codes, programmer
can write one sort() function and pass the datatype as a parameter.
It uses keyword like ‘template’ and ‘type_name’ or ‘class’.
Types of templates:
1. Function Templates (Generic Function- Template for Function)
2. Class Templates (Generic Class – Template for class)
How templates works
Templates are expanded at compile time as macros. The difference is,
the compiler does type-checking before template expansion.
The source code contains only function/ class.
But the compiled code may contain multiple copies of the same
function/class.
Function Templates
A function templates or generic function can be used for different data
types.
Syntax:
template <class/ typename Ttype> return_type function_name
(parameter_list)
{
//body of function
}
, class : It is a keyword is used to specify a generic type in a template
declaration.
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 with the actual data type.
Example 1
#include<iostream>
using namespace std;
template<typename T>T mymax(T x, T y)
{
return (x>y)?x:y;
}
main()
{
cout<<mymax<int>(3,7)<<"\n";
cout<<mymax<double>(3.0,7.0)<<"\n";
cout<<mymax<char>('g','e');
}
Output
7
7
g