C++ Programs‎ > ‎

Simple Program for Function Template Using C++ Programming

C++ Simple Example Programs For Beginners

To swap the numbers using the concept of function template.


STEP 1:  Start the program.
STEP 2:  Declare the template class.
STEP 3:  Declare and define the functions to swap the values.
STEP 4:  Declare and define the functions to get the values.
STEP 5:  Read the values and call the corresponding functions.
STEP6:   Display the results.
STEP 7:  Stop the program. 


template<class t>
void swap(t &x,t &y)
   t temp=x;
void fun(int a,int b,float c,float d)
   cout<<"\na and b before swaping :"<<a<<"\t"<<b;
   cout<<"\na and b after swaping  :"<<a<<"\t"<<b;
   cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
   cout<<"\nc and d after swaping  :"<<c<<"\t"<<d;

void main()
    int a,b;
    float c,d;
    cout<<"Enter A,B values(integer):";
    cout<<"Enter C,D values(float):";


Enter A, B values (integer): 10  20
Enter C, D values (float):    2.50  10.80
A and B before swapping: 10 20
A and B after    swapping:  20 10
C and D before swapping: 2.50  10.80
C and D after               swapping: 10.80  2.50