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;
}

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *