Templates help in defining generic classes and functions and
hence allow generic programming. Generic programming is an approach where
generic data types are used as parameters and the same piece of code work for
various data types.
hence allow generic programming. Generic programming is an approach where
generic data types are used as parameters and the same piece of code work for
various data types.
Function templates are used to create family of functions
with different argument types. The format of a function template is shown
below:
with different argument types. The format of a function template is shown
below:
template<class T>
return_type function_name (arguments of type T)
{
. . . .
.
.
. . . .
.
.
}
I have written a program below which will swap two numbers
using function templates.
using function templates.
#include<iostream>
using namespace std;
template <class T>
void swap(T&a,T&b) //Function Template
{
T temp=a;
a=b;
b=temp;
}
int main()
{
int x1=4,y1=7;
float x2=4.5,y2=7.5;
cout<<“Before Swap:”;
cout<<“nx1=”<<x1<<“ty1=”<<y1;
cout<<“nx2=”<<x2<<“ty2=”<<y2;
swap(x1,y1);
swap(x2,y2);
cout<<“nnAfter Swap:”;
cout<<“nx1=”<<x1<<“ty1=”<<y1;
cout<<“nx2=”<<x2<<“ty2=”<<y2;
return 0;
}
There are so many things that I have missed in this
tutorial. In below video templates are explained very nicely. I am sure that
after watching this video you will understand templates very easily.
tutorial. In below video templates are explained very nicely. I am sure that
after watching this video you will understand templates very easily.
Try to do it without
T temp
😉
Yes this can be done without temp as shown below.
a=a+b;
b=a-b;
a=a-b;
without using arithmetic operators as well as temporary variable
yes, it’s possible. we can solve this without arithmetic operator by the help of Bitwise operator XOR
this program has error ..Is some thing wrong in it..
there is an error in the above program. actually you don’t need the single point function. hi Pallavi please do not follow him.
hii
i am shantanu i use the program in c++ it is so simple program than i whould like prasiend by shree karni clz.
please change the swap to ‘S’wap. It works properly.
use shorthand for swapping:
a = a + b – (b =a);
in this code “swap(m,n)” and “swap(a,b)” shows error of being ambiguous. why?
Because swap is also a predefine function in c++. change the name of function ; Like my_swap
Is it necessary to add &sign inFunction definition
#include
using namespace std;
class temps
{
template
void swap(x &a,y &b)
{
x temp;
temp=a;
a=b;
b=temp;
}
};
int main()
{
int i=2,j=20;
float a=12.8,b=86.36;
char c=’x’,d=’f’;
cout<<"\n"<<i<<"\t"<<j<<"\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\n";
swap(i,j);
swap(a,b);
swap(c,d);
cout<<"\n"<<i<<"\t"<<j<<"\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\n\n\n";
return 0;
}
Try this
there are so many errors in this program…i got 36 errors for this…
it doesn’t work…
It’s not working correctly please correct the corrections
Try to do it without mistakes
😏
#include
using namespace std;
template
void Swap(T &n1,T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}
int main()
{
int i1 = 20, i2 = 40;
float f1 = 12.5, f2 = 23.7;
char c1 = ‘c’, c2 = ‘d’;
cout << "Before swapping : i1 ="<< i1 << "\ti2 = "<< i2 << endl;
cout << "Before swapping : f1 ="<< f1 << "\tf2 = "<< f2 << endl;
cout << "Before swapping : i1 ="<< c1 << "\ti2 = "<< c2 << endl;
Swap(i1,i2);
Swap(f1,f2);
Swap(c1,c2);
cout << "After swapping : i1 ="<< i1 << "\ti2 = "<< i2 << endl;
cout << "After swapping : f1 ="<< f1 << "\tf2 = "<< f2 << endl;
cout << "After swapping : i1 ="<< c1 << "\ti2 = "<< c2 << endl;
return 0;
}
output:
Before swapping : i1 =20 i2 = 40
Before swapping : f1 =12.5 f2 = 23.7
Before swapping : i1 =c i2 = d
After swapping : i1 =40 i2 = 20
After swapping : f1 =23.7 f2 = 12.5
After swapping : i1 =d i2 = c