A template is a generic function or class which can deal with arbitrary types of data.
Consider the linked list picture below.#include <iostream> using namespace std; int maxi(int a, int b) { return a > b ? a : b ; }This condensed code makes use of the ?: ternary operator, also referred to as the conditional operator.
int maxi(int a, int b)
{
if (a > b)
return a;
else
return b;
}
Function templates are implemented like regular functions, except they are prefixed with the keyword template. The following demonstrates in red how we had modified the regular function maxi to make it a function template:
#include <iostream> using namespace std; template <typename T> T maxi(T a, T b) { return a > b ? a : b ; }
(To see a side by side comparison of this function template with the regular function, click here)
You can note the following things:
Using function templates is very easy: just call them like regular functions. When the compiler sees an instantiation of the function template, for example: the call maxi(10, 15) in function main, the compiler generates a function maxi(int, int). Similarly the compiler generates definitions for maxi(char, char) and maxi(float, float) in this case.
#include <iostream> using namespace std ; //maxi returns the maximum of the two elements template <typename T> T maxi(T a, T b) { return a > b ? a : b ; } void main() { cout << "maxi(10, 15) = " << maxi(10, 15) << endl ; cout << "maxi('k', 's') = " << maxi('k', 's') << endl ; cout << "maxi(10.1, 15.2) = " << maxi(10.1, 15.2) << endl ; }
maxi(10, 15) = 15 maxi('k', 's') = s maxi(10.1, 15.2) = 15.2
template <typename T> T maxi(T & a, T & b);
Consider the integer linked list class, which may be represented by this code. The problem with this class is that it handles only integers.
What if we want a linked list with other types (char, float, etc)?
We could create separated classes. For example:template <typename Ttype> class Class_name { ... ... }
template <typename Ttype> return_type Class_name<Ttype>::Function_name { ... ... }
Class_name <type> obj;
(To see a side by side comparison of this class template with an ordinary class, click here)
#include <iostream> using namespace std; //1. creating a class template template <typename T> class my_class { T i; public: my_class(T a); void show(); }; //2. defining member function for class template template <typename T> my_class<T>::my_class(T a) { i=a; } template <typename T> void my_class<T>::show() { cout << "i is: " << i << endl; } int main() { //3. creating specific instances my_class <int> intobj(10); my_class <char> charobj('A'); intobj.show(); charobj.show(); return 0; }
i is: 10 i is: AIn the definitions of intobj and charobj, the data type is enclosed in angle brackets. This data type is the actual parameter (or argument) to the template.
At compile time, the compiler generates (instantiates) two distinct class types and gives its own internal name to each type. You might imagine that the definitions are transformed internally into something like this:
The file: swapperclass.cppswapperclass.cpp contains a class and implementation for swapping two integer values.
Your primary task for this exercise is:
Steps include:
First Value is: 3 Second Value is: 48 AFTER SWAP First Value is: 48 Second Value is: 3 First Value is: 5.5 Second Value is: 8.5 AFTER SWAP First Value is: 8.5 Second Value is: 5.5 First Value is: a Second Value is: b AFTER SWAP First Value is: b Second Value is: a Press any key to continue