C++ program to use single Function Template

//define a single function which returns
//larger integer if two integers are passed,
//and larger character if two characters are passed

#include <iostream>
using namespace std;
template <class L>
L Larger(L x, L y)
{
    if(x>y)
        return x;
    else
        return y;
}
int main()
{
    int i1,i2;
    char ch1,ch2;
    cout<<endl<<"Enter two integers: ";
    cin>>i1>>i2;
    cout<<endl<<"Enter two characters: ";
    cin>>ch1>>ch2;
   
cout<<endl<<"Among integers, the larger is "<<Larger(i1,i2);
    cout<<endl<<"Among characters, the larger is "<<Larger(ch1,ch2);
    return 0;
}

No comments:

Post a Comment