Create a generic function template - C++ template

C++ examples for template:template function

Description

Create a generic function template

Demo Code

#include <iostream>
using namespace std;
template <class T>
void showabs(T number)
{
   if (number < 0)
      number = -number;/*from   ww w .  j  a  v a  2  s .com*/
   cout << "The absolute value of the number is "
   << number << endl;
   return;
}
int main()
{
   int num1 = -4;
   float num2 = -4.23f;
   double num3 = -4.23456;
   showabs(num1);
   showabs(num2);
   showabs(num3);
   return 0;
}

Result


Related Tutorials