Simple Function Template Program Example Get Maximum Number
Generic Programming
Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types
Function Template Definition
A function template behaves same as normal function except that the template works with different data types.
Function Template Syntax
template <class identifier> function_declaration;
template <typename identifier> function_declaration;
templete_identifier fn_name(templete_identifier .. args) {
return ...
}
Example Program
// Header Files
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
// Template Declaration
template<class T>
// Template Function
T getMaximun(T x, T y) {
if (x > y)
return x;
else
return y;
}
int main() {
int a, b, i;
float c, d, j;
cout << "Function Template Programs : Get Maximum Number \n";
cout << "Enter A,B values(integer):";
cin >> a>>b;
i = getMaximun<int>(a, b);
cout << "Result Max Int : " << i;
cout << "\n\nEnter C,D values(float):";
cin >> c>>d;
j = getMaximun<float>(c, d);
cout << "Result Max Float : " << j;
getch();
return 0;
}
Sample Output
Function Template Programs : Get Maximum Number
Enter A,B values(integer):56
89
Result Max Int : 89
Enter C,D values(float):17.99
9.01
Result Max Float : 17.99
Templates In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming