Pages

100 Contemporary Living Room Designs

Friday, March 29, 2013

Template Function

Write a template function that swaps the values of two arguments passed to it. In main( ), use the function with integers and characters.

#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
    T temp=a;
    a=b;
    b=temp;
}
void func(int x,int y,char w,char z)
{
    cout<<"x and y before swap: "<<x<<y<<"\n";
    swap(x,y)
    cout<<"x and y after swap: "<<x<<y<<"\n";
    cout<<"w and z before swap: "<<w<<z<<"\n";
    swap(w,z)
    cout<<"w and z after swap: "<<w<<z<<"\n";

}
int main()
{
    fun(10,20,’s’,’S’);
    return 0;
}



You might also like:-

No comments:

Post a Comment