Program On Templates
"Nice One Program"
About:Template are used to make Any function,class to capable of Handling Any Data Type
Like here in this Example the compiler would Create Different function for Every type With same body.And can handle different Data Types.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
template<class T>
T abs(T n)
{
return (n<0)?-n:n;
}
void main()
{ clrscr();
int a=10;
int b=-6;
float f=-4.5;
long int la=-444444;
double fd=-0.20001;
cout<<"Abs val: of a:"<<abs(a)<<endl;
cout<<"Abs val: of b:"<<abs(b)<<endl;
cout<<"Abs val: of f:"<<abs(f)<<endl;
cout<<"Abs val: of la:"<<abs(la)<<endl;
cout<<"Abs val: of fd:"<<abs(fd)<<endl;
getch();
}
"Nice One Program"
About:Template are used to make Any function,class to capable of Handling Any Data Type
Like here in this Example the compiler would Create Different function for Every type With same body.And can handle different Data Types.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
template<class T>
T abs(T n)
{
return (n<0)?-n:n;
}
void main()
{ clrscr();
int a=10;
int b=-6;
float f=-4.5;
long int la=-444444;
double fd=-0.20001;
cout<<"Abs val: of a:"<<abs(a)<<endl;
cout<<"Abs val: of b:"<<abs(b)<<endl;
cout<<"Abs val: of f:"<<abs(f)<<endl;
cout<<"Abs val: of la:"<<abs(la)<<endl;
cout<<"Abs val: of fd:"<<abs(fd)<<endl;
getch();
}