Write a C++ program to implement function template.

Algorithm:



Step 1: Start the program.
Step 2: Create the function template, to find the maximum of two numbers.
Step 3: Get different data type values like integer, float, and character as the argument to the function.
Step 4: Find the maximum of those data using template function.
Step 5: Display the result Step.
Step 6: Stop the program.

Program Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
template<class t>
t max(t a,t b)
{
if (a>b)
return a;
else
return b;
}
void main()
{
clrscr();
char ch1,ch2; int a,b;
cout<<"Enter two characters:";
cin>>ch1>>ch2;
cout<<"Maximum of "<<ch1 <<” , “<<ch2 <<”is :”<<max(ch1,ch2)<<endl;
cout<<"Enter 2 integers:";
cin>>a>>b; cout<<"Maximum of"<<a<<” „ ”<<b<<” is :”<<max(a,b)<<endl;
getch();
}




Output:
Enter two characters: z a
Maximum of z , a is : a
Enter 2 integers: 25 35
Maximum of 25 , 35 is : 35
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: