Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// template pointers in C++
#include<iostream>
using namespace std;
template<typename T> void myswap(T *a, T *b);
int main()
{
system("cls");
int a=500,b=200;
char ch1='X',ch2='Y';
cout<<" \n Before swaping: "<<a << " "<<b;
myswap(&a,&b); // call by reference
cout<<" \n After swaping: "<<a << " "<<b;
cout<<" \n Before swaping: "<<ch1 << " "<<ch2;
myswap(&ch1,&ch2); // call by reference
cout<<" \n After swaping: "<<ch1 << " "<<ch2;
double s1=12.90 , s2=10.77;
cout<<" \n Before swaping: "<<s1 << " "<<s2;
myswap(&s1,&s2); // call by reference
cout<<" \n After swaping: "<<s1 << " "<<s2;
return 0;
}
template<typename T> void myswap(T *a, T *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}