C++ Program on Templates

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Program for class tempalte 
#include<iostream>
#define clrscr() system("cls")
using namespace std;
template<class T,class V>
class Test
{
    T n;
    V n1;
    public:
    Test(T m,V m1)
    {
       n=m;
       n1=m1;
    }
    void display()
    {
        cout<<n<<"   "<<n1<<"\n";
    }
};
int main()
{
    clrscr();
    Test<int,char> T1(123,'P');
    Test<double,int> T2(12.66,789);
    T1.display();
    T2.display();
}

Program 2

// Program for function tempalte 
#include<iostream>
#define clrscr() system("cls")
using namespace std;


//template<class T> void display(T m);

//template<class T> int search(T ar[],T s,int n);
template<class T> void swap(T a,T b);
int main()
{
      clrscr();
      int a=10,b=40;
      char ch1='x',ch2='y';
      swap('X','Y');
    //  int a[]={10,20,2,34,55,66,77,88}; 
    //  char ch[]={'p','r','s','a','y'};
     
     //if(search(a,334,8))
    //    if(search(ch,'p',5))
    //      cout<<"Searching success";
    //   else
    //      cout<<"Searching  not success";
   return 0;   
}
// template<class T> int search(T ar[],T s,int n)
// {
//        int i;
//        for(i=0;i<n;i++)
//        {
//           if(ar[i]==s)
//             return 1;
//        }
//        return 0;

// }
template<class T> void swap(T a, T b)
{
             T c;
          cout<<"\n Before sawping: "<<a << " "<<b;   
          c=a;
          a=b;
          b=c;
        cout<<"\n After sawping: "<<a << " "<<b;     

}

 

Did you like our efforts? If Yes, please give DataFlair 5 Stars 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 *