Вы находитесь на странице: 1из 17

Introduction

Templates is a concept which enable us to define generic classes and functions and thus support for generic programming .

What is Generic Programming ?


Generic programming is an approach where generic types are used as parametars in algorithms so that they work for a veriety of suitable data types and data structures . A template can be used to be create a family of classes or functions .

A short History
` ` `

Templates are not the feature of original c++ It was added in 1994 by Bjarne Stroustrup The 1st thought of generic programming was introduced by the nephew of Babage in time of developing the Language ADA in 1983.

Classification of templates in C++


C++ provides two kinds of templates: A. class templates and B. function templates
`

The Class Template


o

Class Templates: ` Allow type-specific versions of generic classes Format: template <class T> class ClassName{ definition }

Class Template : Features


` `

Need not use "T", any identifier will work To create an object of the class, type ClassName< type > myObject; Example: Stack< double > doubleStack;

Class Template : Example


`

Example: #include<iostream.h> template<class T1,class T2> class Test { T1 a; T2 b; Public: Test(T1 x,T2 y) { a=x; b=y; }

Class Template : Example


Void show() { Cout<<a<<and<<b<<\n; } }; int main() { Test<float,int> test1(1.23,123); Test<int,char> test2(100,w); test1.show(); //output: test2.show(); //1.23 and 123 return 0; //100 and w }

Class Template : Example(an generic example)


`

Template class functions


Declared normally, but preceded by template<class T> ` Generic data in class listed as type T ` Binary scope resolution operator used ` Template class function definition: template<class T> MyClass< T >::MyClass(int size) { myArray = new T[size]; }
`
`

Constructor definition - creates an array of type T

Function Template
Function Template :
`

A function template is not an actual function, but instead is a pattern for what could become a function

Format: template<class t> return_type function_name (arguments of type T) { //body of function //with type t //when ever appropriate //. }

Function Template : Example


Example: #include<iostream.h> template <class T> void swap(T &x,T &y) { T temp=x; x=y; y=temp; } void fun(int m,int n,float a,float b) { cout<<m and n before swap:<<m<< <<n; swap(m,n); cout<<After swap m and n<<m<< <<n;

Function Template : Example


cout<<a and b before swap:<<a<< <<b<<\n; swap(a,b); cout<<After swap a and b:<<a<< <<b<<\n; } int main() { fun(100,200,11.22,33.44); return 0; }

Function Template : Example


`

Output: m and n before swap:100 200 m and n after swap:200 100 a and b before swap:11.22 33.439999 a and b after swap:33.439999 11.22

Advantages
` ` `

Easily create a large range of related functions or classes Function template - the blueprint of the related functions Template function - a specific function made from a function template

Disadvantage
Creates large range of codes to handle all kind of situations(Exceptions).Which may cause allocation of extra memory space.

Conclusion
The generic programming is the highest level of any programming language though it creates a lot of extra codes which sometime may cause some unnecessary memory blockage. In my opinion its a great feature and we can use it as we need . The flexibility of codes reaches the highest standard by the use of templates.

Thank you

Вам также может понравиться