|
|
A Template functions
|
|
The C++ Function templates are the functions that can handle a different data types
without any separate code for all of the datatypes. For the similar operation on the
several kinds of the data types, the programmer may need not write the different
versions by function overloading. C++ template based function is enough, it will take care
of all data types.
|
|
Let us cosider a small example for the Add function. If requirement is to use the Add
function to both types that is an integer and float type, then the two functions needs
to be created for each data type.
|
int Add(int a,int b)
{
return a+b;
}
// function Without C++ template
float Add(float a, float b)
{
return a+b;
}
// function Without C++ template
|
|
|
If the data types are more than two then it is difficult to be handled,
Because those many number of functions are to be added.
If we make use of the c++ function template, whole process will be reduced to
the single c++ function template. Here is the code fragment for the Add function.
|
template <class T>
T Add(T a, T b)
//C++ function template sample
{
return a+b;
}
|
|
|
The Class Templates
|
|
A C++ Class Templates are been used where we have the multiple copies of the code for
the different data types having the same logic. If the set of functions or the classes
have a same functionality for the different data types, they will become the good
candidates being written as the Templates.
|
|
A C++ class template declaration must starts with the keyword "template". The parameter
must be included inside the angular brackets. Parameter inside a angular brackets,
can either be the keyword class or the typename. This is then followed by a class body
declaration with a member data and the member functions. Following code is the
declaration for the sample Queue class.
|
//Sample code snippet for C++ Class Template
template <typename T>
class MyQueue
{
std::vector data;
public:
void Add(T const &d);
void Remove();
void Print();
};
|
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords CPP Templates, c++ templates, windows templates, c# templates, cpp reference,
python templates, gcc templates, cpp classes, java templates, cpp tutorial,
stl templates, function templates, vector templates, free templates, cpp programming,
functions cpp, library templates, cpp c++, cpp vector, cpp stl, cpp compiler,
string cpp, cpp exe, cpp define, header templates, window templates, reference templates,
programming templates, link templates, windows cpp
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
| Database Quizes |
|
|
| Operating System Quizes |
|
|
| Software Testing Quizes |
|
|
| SAP Module Quizes |
|
|
| Networking Programming Quizes |
|
|
| Microsoft Office Quizes |
|
|
| Accounting Quizes |
|
|
| Computer Basics Quizes |
|
|
|