Q. Write a C++ program to demonstrate the addition of multiple types of data using generic function or template.
Answer:
Output:

Answer:
- Generic template defines a set of statements which are common for the different data types.
- In the above program, first data is passed as integer numbers and second data is passed as float numbers
- Generic template claims both the parameters to be of same data type which means we cannot enter the parameters with the different data types such as integer and float.
- Integer and float are different, the program will show the error. So therefore, it requires the parameters to be of same data type.
#include<iostream>
using namespace std;
template <class Test> Test addition(Test num1, Test num2) //Created generic template
{
return (num1+num2);
}
int main()
{
cout<<"\n Addition of Multiple Types of Data using Generic Template";
cout<<"\n ---------------------------------------------------------";
cout<<"\n Addition of Integer Parameters : "<<addition(10, 10);
//Data is passed as Integer
cout<<"\n Addition of Float Parameters : "<<addition(15.5, 20.10);
//Data is passed as Float
//Generic template claims both the parameters to be of same data type.
return 0;
}
Output:



