In: Computer Science
What are templates in c++? How are they made? Write a program to explain function template.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
A template is a basic but then exceptionally incredible asset in C++. The straightforward thought is to pass information type as a boundary with the goal that we don't have to compose a similar code for various information types. For instance, a product organization may require sort() for various information types. Instead of composing and keeping up the various codes, we can think of one sort() and pass information type as a boundary.
The overall type of a template work definition is appeared here −
template <class type> return_type function_name(arguments list) {
// body of function
}
#include <iostream>
using namespace std;
template <typename T>
T fmax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout << fmax<int>(35, 79) << "\n";
cout << fmax<double>(37.05, 74.70) << "\n";
cout << fmax<char>('w', 'g') << "\n";
return 0;
}