Skip to main content

Templates

TEMPLATES


C++ gives us the facility to write a common function that is independent of the data type but which embodies the common set of instruction which could be applied on any data type.

This has some advantages, like ease of code development and code maintenance.

This could be accomplished with the help of templates. Inside the templates we can have some or all data types unspecified. (Any data type is accepted.)

Templates could be created for individual function or a class. Template function is called generic function and class is called generic class.

GENERIC FUNCTION

The syntax for creating a template for generic function is as follows:

Template
Return_type function_name (T arg1, T arg2, T arg3……….)
{

// function statements
}

It should begin with the keyword template.
Ttype is the placeholder name for a data type used by th function.


























Prog1:

#include

template
void swapdata(X & a, X & b)
{
X temp;
temp = a;
a = b;
b = temp;
}

void main()
{
int i=10, j=20;
float x=10.1, y=20.2;
char a='p',b='q';

cout<<"i ="<
Class class_name
{
……………
……………
};

Once the generic class is created, we can create its object as follows:
Class_name ob;
Prog 3

#include

template class data
{
T a,b;
public:
void setdata(T m,T n)
{
a=m;
b=n;
}
void add_display()
{
cout<d1;
d1.setdata(10,20);
d1.add_display();

datad2;
d2.setdata(10.1,20.2);
d2.add_display();
}
THE STANDARD TEMPLATE LIBRARY

The standard implementation of c++ provides a set of header files where a large number of useful class templates have been defined. These files contain definitions of the class templates, their member functions and number of global associated functions. This library of class templates and its global functions is known as Standard Template Library (STL).

The most commonly used template classes are as follows:

1. list class
2. vector class
3. pair class
4. map class
5. set class
6. multimap class
7. multiset class

1. List class
It is used to create a sequential container. The objects of this class could be defined as follows

list clist;
list flist;
list ilist;

for using this we have to include the library

#include

This uses doubly linked list for storing the elements.

The number of elemtens s list object should have can be specified as follows.

list ilist(3);

The default value can be specified as follows:

List ilist(3, -1);

Member functions of the list class.

1. Push_front(value)
List ilist;
Ilist.push_front(1);

2. Push_back(value)
3. pop_front(value)
4. pop_back(value)
5. iterator
6. insert(position,value)
7. find(lower limit, upper limit, value)
8. size()
9. erase(value)
10. clear()
11. empty()

Comments

Popular posts from this blog

FOR MY BLOG READER'S

It is not for me but for my readers. 'One' resolution fROM taday: I want to look like Aamir khan (without 8 packs) Bike or Car (and why): Car, I’ve three reasons for it 1. It’s safer than bike 2. I’ve always dreamt of going on a long drive with my soul-mate in a car. 3. While driving a car you can look into the eyes of.......... obviously you can guess?? J A chance to fly or a chance to be invisible (and why): Invisible, I suffer from Acrophobia (an extreme or irrational fear of heights). I would love to love: My parents, they deserve my love more than anyone in this world. Most recent Dream I had: I’m a kid again. Teacher in the school asks to describe a word and I was struggling L If I were to marry today, I would: If the girl is pretty, I would be delighted. If the girl is not so ..., I wouldn’t marry and convince everybody that I’m underage (I’m actually 20 years old). My Dream Girl is/would be: SHEEEE Best F...

100+ Sweet Things to Say to Your Girlfriend

Sweet Things to Say to Your Girlfriend : You know what is the basic reason of splitting up of many couples? It does not happen because they do not love each other enough now, but it happens because they forget to show their love to each other. In a relationship, mostly girls demand some attention and affection from their partners. And all you guys can give it to them with the help of sweet things to say to your girlfriend. Don’t let your ego ever comes between you two. Love has more power than any other thing in the world. Use this power to overcome all the problems in your relationship. These sweet things to say to your girlfriend are nothing when you are not feeling them. These are not just the lines but the feelings you hold in your heart for her but could not display all this while. 100+ Sweet Things to Say to Your Girlfriend To help you more on this, I have compiled a list comprising of more than 100 sweet things to say to your girlfriend. These are simpl...

Geometric Print Sarees to Be the Best Choice for Young Women of India

Today, when great number of women population have move towards the western outfits, there are yet thousands and thousands women wear sarees in the country. This is the reason why saree does not be outdated at all. This is one of the most demanded traditional outfits created in amazing designs and shades. Today, wide range of  machine work sarees  is available in the market If you are a young woman and like to dress yourself in the most fashionable way then you must go with the range of online geometric sarees available at many shopping destinations. Besides, if you are recently married women or working women with saree as the formal then geometric can be the best to go with. These sarees are designed in such way that they maintain simplicity along with the stylishness, have a look on some samples of  geometric print sarees  are giver here- Are not these sarees striking and heart-stealing? These are especially designed for them women of 21 st ...