Template in C++
Template in C++
A template in c++ allows the construction of a family of template functions and classes to
perform the same operation on different data types. The templates declared for functions are
called class templates. They perform appropriate operations depending on the data type of the
parameters passed to them.
Template
A template can be considered as a kind of macro. When an object of a specified type is defined
for actual use, the template definition for that class is substituted with the required data type.
Since a template is defined with the parameter that would be replaced by a specified data type
at the time of actual use of the class or function, the templates are sometimes called
parameterized classes or functions.
Function Templates:
A function template specifies how an individual function can be constructed.
template <class T>
return type functionnm(T arg1,T arg2)
{
fn body;
}
For example 1:
Input two number and swap their values
#include <iostream>
using namespace std;
template<class T>
void swapme(T &x, T &y)
{
T temp = x;
x = y;
y = temp;
}
int main()
{
int a, b;
cout<<"Enter two integer values: ";
cin>>a>>b;
swapme(a, b);
cout<<"After swap a = "<<a<<", b = "<<b<<endl;
double p, q;
cout<<"Enter two double values: ";
cin>>p>>q;
swapme(p, q);
cout<<"After swap p = "<<p<<", q = "<<q;
return 0;
}
Output:
Enter two integer values: 10 20
After swap a = 20, b = 10
Enter two double values: 2.4 6.3
After swap p = 6.3, q = 2.4
Example 2:
find maxium between two data items.
#include<iostream>
using namespace std;
template<class T> // Template Declaration
T getMaximun(T x, T y) // Template Function
{
if (x > y)
return x;
else
return y;
}
int main() {
int a, b, i;
float c, d, j;
return 0;
}
Output:
Enter A,B values(integer): 56
89
Result Max Int : 89
Syntax:
template <class T>
class classnm
{
T member1;
T member2;
…
…
public:
T fun();
…
..
};
Example:
Input n numbers into an array and print the element is ascending order. (array sorting)
#include<iostream>
using namespace std;
template <class T>
class array
{
T *a;
int n;
public:
void getdata()
{
int i;
cout<<"Enter how many no:";
cin>>n;
a=new T[n];
for (i=0;i<n;i++)
{
cout<<"Enter a number:";
cin>>a[i];
}
}
void putdata()
{
for(int i=0; i<n ; i++)
{
cout<<a[i]<<endl;
}
}
void sort( )
{
T k;
int i,j;
for(i=0;i<n-1;i++)
{
for (j=0;j<n;j++)
{
if (a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
}
};
int main()
{
array <int>x;
x.getdata();
x.sort();
x.putdata();
array <float> y;
y.getdata();
y.sort();
y.putdata();
}
LECTURE-39
Virtual destructors:
Just like declaring member functions as virtual, destructors can be declared as virtual,
whereas constructors can not be virtual. Virtual Destructors are controlled in the same way
as virtual functions. When a derived object pointed to by the base class pointer is deleted,
destructor of the derived class as well as destructor of all its base classes are invoked. If
destructor is made as non virtual destructor in the base class, only the base class’s destructor
is invoked when the object is deleted.
#include<iostream>
#include<string.h>
using namespace std;
class father
{
protected:
char *fname;
public:
father(char *name)
{
fname=new char(strlen(name)+1);
strcpy(fname,name);
}
virtual ~father()
{
delete fname;
cout<<"~father is invoked…";
}
int main()
{
father *basep;
basep =new father ("mona");
cout<<"basep points to base object…";
basep->show();
delete basep;
basep=new son("sona","mona");
cout<<"base points to derived object…";
basep->show();
delete basep;
}
#define size 5
class vector
{
int v[size];
public:
vector();
friend vector operator*(int a,vector b);
friend vector operator *(vector b,int a);
friend istream &operator>>(istream &,vector &);
friend ostream &operator<<(ostream &,vector &);
};
vector :: vector()
{
for(int i=0;i<size;i++)
v[i]=0;
}
vector::vector(int *x)
{
for (int i=0;i<size;i++)
v[i]=x[i];
}
vector operator*(int a,vector b)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
}
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
}
istream &operator>>(istream &din,vector &b)
{
for(int i=0;i<size;i++)
din>>b.v[i];
}
ostream &operator<<(ostream &dout,vector &b)
{
for(i=0;i<size;i++)
dout<<a[i];
return dout;
}
int x[size]={2,4,6};
int main()
{
vector m;
vector n=x;
cout<<”enter elements of vector m”;
cin>>m;
cout<<m;
vector p,q;
p=2*m;
q=n*2;
cout<<p;
cout<<q;
LECTURE-40
Managing Console I/O