0% found this document useful (0 votes)
28 views9 pages

Template in C++

The document discusses templates in C++ which allow creating reusable components like functions and classes that can work with different data types. It provides examples of function templates, class templates and overloading operators like >> and << for a class. Virtual destructors are also covered which ensure proper cleanup of memory when deleting objects through base class pointers.

Uploaded by

Vani Vats
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

Template in C++

The document discusses templates in C++ which allow creating reusable components like functions and classes that can work with different data types. It provides examples of function templates, class templates and overloading operators like >> and << for a class. Virtual destructors are also covered which ensure proper cleanup of memory when deleting objects through base class pointers.

Uploaded by

Vani Vats
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Template:

Template supports generic programming, which allows developing reusable software


components such as functions, classes, etc supporting different data types in a single
framework.

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

Generic Function Generic Class

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;

cout << "Enter A,B values(integer):";


cin >> a>>b;
i = getMaximun<int>(a, b);
cout << "Result Max Int : " << i;

cout << "\n\nEnter C,D values(float):";


cin >> c>>d;
j = getMaximun<float>(c, d);
cout << "Result Max Float : " << j;

return 0;
}
Output:
Enter A,B values(integer): 56
89
Result Max Int : 89

Enter C,D values(float) :17.99


9.01
Result Max Float : 17.99
Overloading of function template
#include<iostream>
using namespace std;
template <class T>
void print( T a)
{
cout<<a;
}
template <class T>
void print( T a, int n)
{
int i;
for (i=0;i<n;i++)
cout<<a;
}
int main()
{
print(1);
print(3.4);
print(455,3);
print("hello",3);
}

Multiple arguments function template:


//find sum of two different numbers
#include<iostream>
using namespace std;
template <class T,class U>
T sum(T a,U b)
{
return a+(U)b;
}
int main()
{
cout<<sum(4,5.5);
cout<<sum(5.4,3);
}
LECTURE-38
Class Template
Similar to functions, classes can also be declared to operate on different data types. Such
classes are class templates. a class template specifies how individual classes can be
constructed similar to normal class definition. These classes model a generic class which
support similar operations for different data types.

Syntax:
template <class T>
class classnm
{
T member1;
T member2;


public:
T fun();

..
};

objects for class template is created like:


classnm <datatype> obj;
obj.memberfun();

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

virtual void show()


{
cout<<"father name…"<<fname;
}
};

class son: public father


{
protected:
char *s_name;
public:
son(char *fname,char *sname):father(fname)
{
sname=new char[strlen(sname)+1];
strcpy(s_name,sname);
}
~son()
{
delete s_name;
cout<<"~son() is invoked"<<endl;
}
void show()
{
cout<<"father’s name"<<fname;
cout<<"son’s name:"<<s_name;
}
};

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

// Overloading of >> and << operator

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

vector operator*(vector b,int a)


{
vector 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

You might also like