Friday, 8 March 2019

CPP-EX19



Write a C++ program to swap two interger values and two float values  by using function Template.

#include<iostream.h>
#include<conio.h>
 template<class T>
 void swap(T &value_1,T &value_2)
   {
    T temp;
    temp=value_2;
    value_2=value_1;
    value_1=temp;
   }
 int main()
   {
    clrscr();
    int int_1,int_2;
    float float_1,float_2;
   
cout<<"\n\n*********** Data Type - Int ***********";
    cout<<"\nEnter the value of int_1=";
    cin>>int_1;
    cout<<"\nEnter the value of int_2=";
    cin>>int_2;
 
  cout<<"\n\n*********** Data Type - Float ***********";
    cout<<"\nEnter the value of float_1=";
    cin>>float_1;
    cout<<"\nEnter the value of float_2=";
    cin>>float_2;

    cout<<"\n\n*********** After Swap ***********";
   
cout<<"\nValues of Int:";
    swap(int_1,int_2);
    cout<<"\nValue of int_1="<<int_1;
    cout<<"\nValue of int_2="<<int_2;
    cout<<"\n\nValues of Floats:";
  
 swap(float_1,float_2);
    cout<<"\nValue of float_1="<<float_1;
    cout<<"\nValue of float_2="<<float_2;
    getch();
    return 0;
   }