i have an assigment and i know nothing about templates and what are they used for?do you know any tutorial
Printable View
i have an assigment and i know nothing about templates and what are they used for?do you know any tutorial
Why don't you read the tutorials on THIS site.
They are used if you want a function or class to work with different types of classes.
for example if you want to write a function minimum, that you will use for int's , floats, and your own classes such as Height you would have to write one function for each datatype.
or if you use a template you only need one function
template < class T >
T minimum( T x, T y) {
return (x < y ? x : y);
}
so pretty much the name for the type ( in this case T ) is filled in with the type you are calling it with
so you can call
int result = minimum( 5, 4);
or somthing like
Height one = new Height(5.5);
Height two = new Height(3.3);
Height minHeight = minimum(one,two);
in other words, now the function will work for any type which the < sign is defined.