Answer to Question #293129 in C++ for sfk

Question #293129

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. 


1
Expert's answer
2022-02-02T06:11:02-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog