Template in C++
04:18 | Author: Unknown

The First i have a question, what is template ? My Opinion template use when we would make a function that not use primitive data type.
For Example we want to make tukar function use primitive data type :

void tukar (int a, int b) {
int temp = a;
a = b;
b = temp;
}

If we use function above, so we only can input each of parameter with integer data type only.
What must we do if we want to make each of parameter can input with many kind of data type ?? The Solution is Use Template


This is Function that using template
template <class T>
void tukar (T a, T b) {
T temp = a;
a = b;
b = temp;
}
Now tukar function can be used with many kind of data type, like integer, float, character, and etc.

|
This entry was posted on 04:18 and is filed under . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

1 comments:

On 8 February 2009 at 23:21 , konixbam said...

deklarasi variable nya dimana>???

template class T
void tukar (T a, T b) {
T temp = a;
a = b;
b = temp;
}


d mana??