Example of Template in C++ Programming Language - Declaration and Use.
This code snippet will declare and access the template. In this example I am going to explain you how to declare and use of a Template in C++.
C++ Code Snippet - Declaration and Use of a Template
/*Example of Template in C++*/
#include <iostream>
using namespace std;
template <class T>
T sum(T a,T b)
{
T s;
s=a+b;
return s;
}
int main()
{
cout<<"Sum of two integers: "<<sum(10,20)<<endl;
cout<<"Sum of two float: "<<sum(1234.f,34.445f)<<endl;
cout<<"Sum of two doubles: "<<sum(10.23,20.45)<<endl;
return 0;
}
Sum of two integers: 30
Sum of two float: 1268.44
Sum of two doubles: 30.68