Write a C++ Program to find Largest among two numbers using function template. Here’s a Simple C++ Program to find Largest among two numbers using function template in C++ Programming Language.
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.
There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..
Function Template :
The general form of a template function definition is shown here:
Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:
template <class type> class class-name
{ . . . }
Below is the source code for C++ Program to find Largest among two numbers using function template which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to find Largest among two numbers using function template */
#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1, i2;
float f1, f2;
char c1, c2;
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
return 0;
}
OUTPUT : :
/* C++ Program to find Largest among two numbers using function template */
Enter two integers:
8 3
8 is larger.
Enter two floating-point numbers:
1.2
5.6
5.6 is larger.
Enter two characters:
f
i
i has larger ASCII value.
Exit code: 0 (normal program termination)
Above is the source code and output for C++ Program to find Largest among two numbers using function template which is successfully compiled and run on Windows System to produce desired output.
Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.
There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..
Function Template :
The general form of a template function definition is shown here:
Class Template :
Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:
Below is the source code for C++ Program to find Largest among two numbers using function template which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
need an explanation for this answer? contact us directly to get an explanation for this answerAbove is the source code and output for C++ Program to find Largest among two numbers using function template which is successfully compiled and run on Windows System to produce desired output.