23

Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

Embed Size (px)

DESCRIPTION

 A template can be created with some or all variables therein having unspecified data types.  Whenever the template is invoked by passing arguments of a certain type, the C++ language on its own replaces the unspecified type with the type of the arguments passed. Syntax for creating a template for a generic function: template return_type function_name (T arg1,..) { // statements }

Citation preview

Page 1: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters
Page 2: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters in algorithms so that they work for a variety of suitable data types and data structures.

void swap( int &a, int &b){ int temp;

temp=a;a=b;b=temp; }

void swap( float &a, float &b){ float temp;

temp=a;a=b;b=temp; }

Page 3: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

A template can be created with some or all variables therein having unspecified data types.

Whenever the template is invoked by passing arguments of a certain type, the C++ language on its own replaces the unspecified type with the type of the arguments passed.

Syntax for creating a template for a generic function:

template < class T, …>return_type function_name (T arg1,..){ // statements }

Page 4: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

#include<iostream.h>#include<conio.h>#include<string.h>class student{

char name[10];int age;public:student(char a[]=0, int b=0){

strcpy(name,a);age=b;

}

Page 5: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

void printdata(){

cout<<endl<<"Name:"<<name;cout<<endl<<"Age:"<<age;

} };template <class T>void swap( T &a, T &b){

T temp;temp=a;a=b;b=temp;

}

Page 6: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

void main(){

clrscr();int x,y; x=10; y=20;cout<<endl<<" BEFORE SWAPPING INTEGERS:"<<x<<" " <<y;swap(x,y);cout<<endl<<" AFTER SWAPPING INTEGERS:"<<x<<" " <<y;float a,b;a=1.1; b=2.2;cout<<endl<<" BEFORE SWAPPING FLOATS:"<<a<<" " <<b;swap(a,b);

Page 7: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

cout<<endl<<" AFTER SWAPPING FLOATS:"<<a<<" " <<b;student s1("aaa",12);student s2("bbb",21);cout<<endl<<" BEFORE SWAPPING OBJECTS:";s1.printdata();s2.printdata();swap(s1,s2);cout<<endl<<"AFTER SWAPPING OBJECTS:";s1.printdata();s2.printdata();getch();

}

Page 8: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

The compiler generates the actual function from a template only once for a given data type.

The compiler first looks for an exact match to resolve the function call before looking for a template.

If it finds an exact match, It does not look for the template.

The entire definition of the function must appear in the header file.

The ‘larger’ template will work for int and float. When used for string, does not work correctly. It should be overloaded with the special version.

FUNCTION TEMPLATES CAN BE OVERLOADED.

Page 9: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

#include<iostream.h> #include<conio.h> #include<string.h>

template <class T> T& larger( T& a, T& b) { return a>b? a:b; } char* larger( char* a,char* b) //overloading { return strcmp(a,b)>0?a:b; }

Page 10: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

void main() { clrscr(); int a=10; int b=20; cout<<endl<<" The Larger number is :“

<<larger(a,b); float c=5.8; float d=3.4; cout<< endl<<" The larger float is:”

<<larger(c,d);char e[5];strcpy(e,"abc");char * f="qwe";char* g=larger(e,f);cout<<endl<<" The larger string is :"<<g;getch();}

Page 11: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

More than one generic type can also be mentioned in the template.

template <class T, class U>void f1(const T & a, const U & b){

// statements}

Page 12: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

#include<iostream.h> #include<conio.h> #include<string.h> template <class T> T& display(const T & a){cout<<endl<<"inside single parameter function;";} template <class T> T& display(const T& a, const T & b){cout<<endl<<" inside the 2 parameter

function;"; } void main() { clrscr(); display(10); display(12,1);

getch();}

Page 13: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

Arises out of the need to have generic classes that handle data of different types.

class x_int{int val;void f1( int &);void f2(int &);};

class x_char{char val;void f1( char &);void f2(char &);};

Page 14: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

The classes are similar , other than the type of their data type.

template<class T>class X{T val;public: void f1( T&);

void f2(T&);};

Page 15: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

Member functions of class templates are defined as follows:

template<class T>void X<T> ::f1(T & p)

{// definition}

Page 16: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

The definition of the template starts with the keyword ‘template’.

The list of template arguments ( type and non-type) are enclosed in angular brackets.

Each type template argument is preceded by the keyword class.

Each non type template argument is preceded by the data type.

Member functions are defined the same way as the template class itself.

In a function definiton, The class name given before the scope resolution operator is followed by the names of all template arguments enclosed in angular brackets.

Page 17: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

Objects of the template class can be declared as follows

X<int> intObj; When the compiler sees the declaration

of the object, it replaces each occurrence of the template argument by the template parameter in the definition of the class template and generates a separate class.

In the preceding case, each occurence of the token T in the class X will be replaced by the keyword ‘int’.

X<int> intObj1,intObj2;intObj1.f1(intObj2);

Page 18: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

The compiler generates the exact definition of a class from a given template once only for each data type and then reuses it.

A template class can take more than one template type argument.template<class T, class U>class X {T val1; U val2; //rest of class X }

It can also take a non-type template argumenttemplate<class T, int v>class X {T val1; //rest of class X }while declaring object of such a class, a data type will be passed as parameter for type tempalte arg. X<int,5>intObj;

Page 19: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

The name of the template argument can not be used more than once in the list of template arg.template<class T, class T> //ERROR

Te same name can be used in two different template classes template<class T>

class X { //defn. of class X } template<class T> class Y { //defn. of class Y } The name of the template argument need not be

the same in the declaration and the definition of the template classtemplate <class T>

class X ; //declaration template<class U> class X { //defn. of class X }

Page 20: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

Formal arguments of the template functions can be objects of the template classtemplate<class T>

class X { //defn. of class X };template<class U>void f1(X<U> v) { definition of the function }

Nested classes can be created for template classes in the same way as of non-template classes.template<class T>

class X { class Y { T x; //rest of class Y };//definition of class X };

Page 21: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

const int MAX = 10; // stack exampleTemplate <class T>class stack{ T stk[MAX]; int top; public: stack() {top=-1;} void push(Tdata) { if (top==MAX-1)

cout<<endl<<“stack is full”; else { top++; stk[top]=data; }

} T pop()

{ if(top==-1) {cout<<endl<< “stack empty”; return NULL; }

Page 22: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

else { T data = stk[top]; top--; return data; } } };class complex{ float real , imag ; public: complex(float r=0.0,float i=0.0) { real = r; imag = i; } friend

ostream&operator<<(ostream&o,complex&c); };ostream&operator<<(ostream&o,complex&c) { o<<c.real<<“\t”<<c.imag; return 0; } }main(){ stack<int>s1; s1.push(10); s1.push(20);

Page 23: Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters

s1.push(30); cout<<endl<<s1.pop(); //3times stack<float>s2;s2.push(3.14); s2.push( 6.28); s2.push( 9.53);cout<<endl<<s2.pop(); cout<<endl<<s2.pop(); cout<<endl<<s2.pop(); complex c1(1.5,2.5),c2(3.5,4.5),c3(-1.5,-2.5);stack<complex>s3s3.push(c1); s3.push(c2); s3.push(c3); cout<<endl<<s3.pop(); cout<<endl<<s3.pop(); cout<<endl<<s3.pop();

}