Template Pointers in C++

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

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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