Create a function Swap using templates, your function should have the following prototype
void swap (t x, t y)
This function should display the swapped values out of the two values being passed, in your main function create variables of different types, integer and character and check what your function returns for each different variable type.
#include <iostream>
#include <string>
using namespace std;
template<typename t>
void swap(t* x, t* y){
t temp= *x;
*x = *y;
*y = temp;
}
int main(){
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "\nAfter swapping" << endl;
swap(&a,&b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
char k = 'a', l = 'b';
cout << "\n\nBefore swapping" << endl;
cout << "k = " << k << endl;
cout << "l = " << l << endl;
cout << "\nAfter swapping" << endl;
swap(&k,&l);
cout << "k = " << k << endl;
cout << "l = " << l << endl;
system("pause");
return 0;
}
Comments
Leave a comment