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

Death of a Star! in Bollywood

When I was flipping through my Diary 2002 the other day, my jottings about Monal’s tragic end beckoned me and brought to my mind a host of awesome nostalgic thoughts. I had a feeling of déjà vu as the dead artist again touched a chord in my heart. How would emotionally deplete artists behave when driven to wall? [Read on…] Monal, a starlet from Bollywood, hung herself to death on the Tamil New Year’s Day. A strange quirk of fate played havoc with her life, putting off her promising career of becoming an icon in the Kollywood. TV visuals showed her sleeping eternally on a bier; she was a feast to flies that were swarming her lissome body … a body that set fire to the hearts of thousands of her fans. Monal’s premature death moved me to a great extent not because she was one of the upcoming actors of the Kollywood and a diva for whom the tinsel world plumped rather madly, but because it set me thinking as to what led the young actor to kill herself savagely in the middle of her

11-awesome-diwali-lighting-decoration-ideas

Beautiful Flower Lights On Water Diwali Decoration Ideas Picture Beautiful Hanging Lighting Lanterns Diwali Decoration Beautiful Lighting Decoration For Diwali Beautiful Lighting Lamps Decoration Cool Diwali Lighting Decoration Ideas Diwali Lighting Decoration Picture Diwali Lighting Decoration Ideas Elegant Hanging Lighting Lamp Decoration Glass Bottles Hanging Lighting Lamps Diwali Decoration Hanging Beautiful Lighting Kandil Diwali Decoration Photo Shubh Labh Lighting Decoration For Diwali

Ssc combined graduate level exam syllabus and exam detail

q Tier-I of the Examination: General Intelligence & Reasoning : It would include questions of both verbal and non-verbal type. This component may include questions on analogies, similarities and differences, space visualization, spatial orientation, problem solving, analysis, judgment, decision making, visual memory, discrimination, observation, relationship concepts, arithmetical reasoning and figural classification, arithmetic number series, non-verbal series, coding and decoding, statement conclusion, syllogistic reasoning etc. The topics are, Semantic Analogy, Symbolic/Number Analogy, Figural Analogy, Semantic Classification, Symbolic/Number Classification, Figural Classification, Semantic Series, Number Series, Figural Series, Problem Solving, Word Building, Coding & de-coding, Numerical Operations, symbolic Operations, Trends, Space Orientation, Space Visualization, Venn Diagrams, Drawing inferences, Punched hole/pattern –folding & unfolding, Figural Pattern – folding