FUNCTION TEMPLATE USING C++


/*PROGRAM USING FUNCTION TEMPLATE*/


PROGRAM

#include<iostream.h>
#include<conio.h>
template<class T>T sum(T *array,int n)
{
T temp=0;
for(int i=0;i<=n-1;i++)
temp=temp+array[i];
return(temp);
}
int sum(int *a,int n);
float sum(float *b,int n);
double sum(double *c,int n);
long int sum(long int *d,int n);
short int sum(short int *e,int n);
void main()
{
clrscr();
int n=3,sum1;
float sum2;
double sum3;
long int sum4;
short int sum5;
static int a[3]={1,2,3};
static float b[3]={1.1,2.2,3.3};
static double c[3]={43.33,32.86,56.33};
static long int d[3]={555,655,755};
static short int e[3]={5,5,5};
cout<<"\n\t\t PROGRAM USING FUNCTION TEMPLATE";
cout<<"\n\t\t _______________________________\n";
sum1=sum(a,n);
cout<<"\n Sum of the integers="<<sum1<<endl;
sum2=sum(b,n);
cout<<"\n Sum of float is="<<sum2<<endl;
sum3=sum(c,n);
cout<<"\n Sum of double ="<<sum3<<endl;
sum4=sum(d,n);
cout<<"\n Sum of long int ="<<sum4<<endl;
sum5=sum(e,n);
cout<<"\n Sum of short int ="<<sum5<<endl;
getch();
}

OUTPUT:


                 PROGRAM USING FUNCTION TEMPLATE
                 _______________________________________

 Sum of the integers=6

 Sum of float is=6.6

 Sum of double =132.52

 Sum of long int =1965

 Sum of short int =15


Comments

Popular posts from this blog

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

BOOKS DETAILS USING C STRUCTURE

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE