Templates

                                         A

                              Quick Reference

                             “ Programming”

 

“ Article By J. James Prasanna 

                                           E-Mail : jaihindprasanna@yahoo.com

 

                                               

 Definition :

 

                 A predefined class or form that is applied to each new class or form you create.

Template is a “Generic Term “ . Template declaration specifies a set of parameterized classes or functions.

 

Main usage for Templates :

 

1) Receive at run time any type of argument ( string , int ,float , ..etc.,) in a

 

  single prototype  declaration.

 

2) Reduce the size ofs Code

 

3) Lots of Built in code like Predefined operations (Standard Template Library) (List ,map, set , ..etc )

 

4) Support OOPS Concept

 

5) Memory Management.

 

Templates specifies  :

 

Class Template and Function Template.

 

 

Class Template :

 

Class templates define a family of related classes that are based on the parameters passed to the class upon instantiation

 

Syntax :

 

template  < typelist > Class Declaration

 

 

Example :

 

#include <iostream>

#include <string>

using namespace std;

 

ostream &newline(ostream&nel){

            return nel<<endl<<endl;

 

}

template < class T>

class James

{

             T Number;

             James *Next;

            public:

                         void Create(James<T> *);

                         void Display(James<T> *);

                         int Print(T); 

 

};

 

template <class T> void James<T>::Create(James<T> *p)

{

            //p=new James<T>;

            cout<<"Enter the Number "<<newline;

            cin>>p->Number;

            cout<<"Enter "<<newline;

            cin>>p->Next->Number;

}

 

template< class T> void James<T>::Display(James<T> *p)

{

cout<<"Answers : "<<newline<<p->Number<<newline<<p->Next->Number<<endl;

}

 

template<class T> int James<T>::Print(T Integer)

{

cout<<Integer <<newline;

return 1;

}

 

void main()

{

James<int> *m=new James<int>;

James<float> *m1=new James<float>;

James<string> *m2=new James<string>;

m2->Create(m2);

m2->Display(m2);

delete m2;

}

In the above program explain the clear idea about  “ class templates “

 

Function Template :

 

Function templates are similar to class templates, but define a family of functions .

Syntax :

 

template  < typelist > Function  Declaration

 

Example :

 

#include <iostream>

#include <string>

using namespace std;

 

template<class T> void f()

{

cout<<"I Won with the Help of C++"<<endl;

}

 

void main()

{

void g();

g();

}

void g()

{

f<int>();

}

 

Standard Template Library  ( STL ) :

 

STL is a general purpose library consist of algorithm and data structure.

It consists of

 

 

Sample Program :

 

Display a string using Vector Template

 

#include <iostream>

#include <vector>

using namespace std;

 

class sample{

public:

            void display()

            {

            cout<<"Display"<<endl;

            }

};

void main()

{

vector<sample> a;

vector<sample>::iterator i=a.begin();

i->display();

}

 

Display a Name and Year using Vector Template

 

#include <string>

 

using namespace std;

 

class sample{

            string name;

            int number;

public:

            sample(){}

            sample(string newname,int newnumber):name(newname),number(newnumber){}

            static void Display(vector<sample>*p)

            {

                        for(vector<sample>::iterator i=p->begin();i!=p->end();i++)

                        {

                        cout<<i->name<<"\t"<<i->number<<endl;

                        }

            }

};

 

void main()

{

            vector<sample> *a=new vector<sample>;

            cout<<endl<< "NAME "<<"\t"<<"YEAR"<<endl<<endl;;

            a->insert(a->begin(),sample("India",23));

            a->insert(a->begin(),sample("James",22));

            a->insert(a->begin(),sample("MACMET",21));

            a->insert(a->begin(),sample("Technologies",20));

            sample::Display(a);

            cout<<endl;

            delete a;

}

 

 

Arithmetic Function Object:

 

Arithmetic Function objects supports standard arithmetic operations

 

For addition Function Object  

Syntax:

 

plus<type >

 

Sample Program :

 

a)           #include <iostream>

  #include <string>

  #include <functional>

 

  using namespace std;

 

  void main()

{

            plus<int> add;

            cout<<add(1,2)<<endl;

            plus<string> add1;

            cout<<add1("james", "  prasanna ")<<endl;

}

 

 

b) #include <iostream>

#include <numeric>

#include <vector>

#include <functional>

 

using namespace std;

 

void main()

{

            minus<int> red;

            negate<int> neg;

            cout<<red(1,2)<<endl;

            cout<<neg(2)<<endl;

}

 

Auto ptr Class :

 

Auto_ptr class avoid memory Leak

 

Syntax:

auto_ptr<type>

 

Sample Program :

 

#include <iostream>

#include <memory>

#include <string>

using namespace std;

 

void main()

{

            auto_ptr<string> a(new string("jshdfjhsd"));

            cout<<*a<<endl;

            a->insert(strlen("  "),"james");

            cout<<*a<<endl;

}

 

Stack Template :

 

#include <stack>

#include <iostream>

using namespace std;

 

void main()

{

            stack<int> a;

            a.push(1);

            if(!a.empty())

                        cout<<"Not Empty "<<endl;

                 else

             cout<<"Empty "<<endl;

            cout<<a.top()<<endl;}

 

 

Set Template :

 

#include <set>

#include <iostream>

using namespace std;

 

void truefalse(int x)

{

            cout<<(x?"true":"false")<<endl;

}

void main()

{

            set<int> a,b;

            a.insert(1);

            a.insert(2);

            a.insert(3);

     set<int>::iterator i;

             for(i=a.begin();i!=a.end();i++)

             cout<<*i<<endl;

             truefalse(a.key_comp()(1,2));

              i=a.find(0);

             truefalse(i!=a.end());

}