Pages

Friday, 15 November 2013

Simple Function Template


Statement: A function template in C ++ that can calculate
                        larger of the two elements where elements can
                        be of any type (i.e.int, char, float etc)


                  Download .cpp file (Source File) of this code


template <typename dataType>
dataType getLarger(dataType value1, dataType value2)
{
       if (value1 > value2)
              return value1;
       else
              return value2;
}

int main()
{
       cout << "Larger of the two integers, 6 and 9 is " <<
       getLarger(6, 9) << endl << endl;

       cout << "Larger of the two characters, J and E is " <<
       getLarger('E', 'J') << endl << endl;

      cout << "Larger of the two floating numbers, 4.4 and 2.7 is " <<
      getLarger(2.7, 4.4) << endl << endl;

       cout << "Larger of the two integers, \"Google\" and \"Bing\" is
       \"" << getLarger("Google", "Bing") <<"\""<< endl << endl;

       system("pause");
       return 0;
}
 

0 comments:

Post a Comment