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

27 Simple mehndi designs for hands for Karwa Chauth

The most  simple mehndi designs for hands  for Karwa Chauth 2014 Karwa Chauth  is around the corner and I am so excited about getting mehndi done! This time I plan to go with a simple yet quirky design. Don’t want my hands full, however it needs to be a style statement for this year’s most awaited festival for us ladies! There are some basic categories of mehndi designs, such as Gujrati, Rajasthani, Marwari,  Arabic  and African  patterns to choose from. They have certain prominent motifs or symbols that distinguishes one from the other. I just can’t seem to stop experimenting with these styles. From  circles to squares , peacocks and paisleys , checks and dots and what not. But my all-time favorite is a simple, curling vine, of course finger tips covered with thick coating of aromatic mehndi (which is so traditional). Quirky patterns include  traditional  as well as non-traditional patterns tweaked to one’s own liking. This includ...

Kick new song Hangover: Salman Khan’s singing stint is impressive!

The  Kick  star has lent his voice for his new song  Hangover The wait is over.  Hangover  is here! Last time, Salman Khan floored us all with his tapori stint in song  Jumme Ki Raat alongside Jacqueline Fernandez who sizzled with her sultry moves, donning a hot red lace dress. It seems along with giving us killer action sequences and  Dabangg  one liners, our very own Sallu  miyaan  can sing as well! The  Jai Ho  star’s highly anticipated singing stint in  Hangover  is finally out and we are happy to report that the Bhaijaan of B-town has certainly pleasantly surprised us all with his voice which has elements of a sexy twang and a Salman’s casual laid back back feel to it. The song on the whole is romantic, with a hint of zing added to it which makes it appealing to the ears. The tune is catchy and the lyrics are simple, which make  Hangover  a pleasant melody overall. We wonder what the video of ...

I'm not back because of Salman: Mahek

Mahek Chahal, who got evicted from the " Bigg Boss" house earlier this month, is back. In conversation with DT, Mahek not only spoke about her fellow contestants accusing  Salman Khan   of promoting favouritism in the show, but also took digs at Akashdeep Saigal for using foul language and misbehaving with her. "It would be unfair to say that I have been selected as the wild card entry because of Salman Khan. Yeh sarasar jhooth aur bakwaas hai. My supporters saw me being humiliated on the show. If I'm back, it is only because of them and not because of Salman," she says. Now that the model-turned-actress has entered the house again, she wants to settle a score with all those who ill-treated her in the house. "There are certain issues which I want to clarify in the house. For instance, Sky maintains that he never abused anyone. Woh kehta hai ki usne logon ko gaali nahin di. But he hasn't only called me 'bhootni', but also bad-mouthed my family...