Fri Dec 06 2019

Template Function

C++ Programming1373 views

File Name: template-function.cpp

#include<iostream>
using namespace std;
template <class temp>

/* overloading template function */
void swap(temp *a, temp *b) {
	temp t = *a;
	*a = *b;
	*b = t;
}

void swap (int *a, int *b) {
	int t = *a;
	*a = *b;
	*b = t;
}

int main() {
	int a = 5;
	int b = 10;

	/* swap two integer values */
	swap(&a,&b);
	cout << "Integer Swap:\n" << a << "\n" << b << endl;

	float c = 5.22;
	float d = 10.95;

	/* swap two float values */
	swap(&c,&d);
	cout << "Float Swap:\n" << c << "\n" << d << endl;

	string e = "Programming";
	string f = "Fun";

	/* swap two strings */
	swap(&e,&f);
	cout << "String Swap:\n" << e << "\n" << f << endl;
	return 0;
}



/* Output */
Integer Swap:
10
5

Float Swap:
10.95
5.22

string Swap:
Fun
Programming
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.