Write a C++ Program to Perform Simple Addition Function Using Templates. Here’s a Simple C++ Program to Perform Simple Addition Function Using Templates 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 Perform Simple Addition Function Using Templates which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to Perform Simple Addition Function Using Templates */
#include <iostream>
#include <stdio.h>
using namespace std;
template <class type> type add(type num1, type num2);
int main ()
{
std::cout << "Please enter integer 1 : ";
int num1;
cin >> num1;
cout << "\nPlease enter integer 2 : ";
int num2;
cin >> num2;
int answer = add(num1, num2);
cout << "\nThe sum of [ " << num1 << " + " << num2 << " ] = " << answer<<"\n";
getchar();
return 0;
}
template <class type> type add(type num1, type num2)
{
type result = num1 + num2;
return result;
}
OUTPUT : :
/* C++ Program to Perform Simple Addition Function Using Templates */
Please enter integer 1 : 12
Please enter integer 2 : 34
The sum of [ 12 + 34 ] = 46
Process returned 0
Above is the source code and output for C++ Program to Perform Simple Addition Function Using Templates 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 Perform Simple Addition Function Using Templates which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code and output for C++ Program to Perform Simple Addition Function Using Templates which is successfully compiled and run on Windows System to produce desired output.
need an explanation for this answer? contact us directly to get an explanation for this answer