C++ Program on Template Pointer
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Program for tempalte pointer
#include<iostream>
#define clrscr() system("cls")
using namespace std;
template<class T> void swap(T *a,T *b);
int main()
{
clrscr();
int a=100,b=50;
char ch1='X',ch2='Y';
double d1=50.10,d2=30.10;
cout<<"\nBefore swaping: "<<a<<" "<<b;
swap(&a,&b); // call by reference using template
cout<<"\nAfter swaping: "<<a<<" "<<b;
cout<<"\nBefore swaping: "<<ch1<<" "<<ch2;
swap(&ch1,&ch2); // call by reference using template
cout<<"\nAfter swaping: "<<ch1<<" "<<ch2;
cout<<"\nBefore swaping: "<<d1<<" "<<d2;
swap(&d1,&d2); // call by reference using template
cout<<"\nAfter swaping: "<<d1<<" "<<d2;
}
template<class T> void swap(T *a,T *b) // template pointer
{
T c;
c=*a;
*a=*b;
*b=c;
}
Did we exceed your expectations?
If Yes, share your valuable feedback on Google

