Write a template function in C that returns a greater number

Write a template function in C++ that returns a greater number of the two integer numbers.

Solution

The template function in C++ with sample program is given below. Comments are also added. Have a look.

#include <iostream>
using namespace std;

// template function to return greater number of the two integers
template <typename T>
T maxInteger (T num1, T num2)
{
    return num1 < num2 ? num2 : num1; //condition operator is used
}

int main()
{
   cout<<maxInteger<int>(10, 15);         // method call to template function
}

Output: 15

Write a template function in C++ that returns a greater number of the two integer numbers.SolutionThe template function in C++ with sample program is given belo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site