Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: " Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters."— Presentation transcript:

1

2  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; }

3  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 }

4 #include class student { char name[10]; int age; public: student(char a[]=0, int b=0) { strcpy(name,a); age=b; }

5 void printdata() { cout<<endl<<"Name:"<<name; cout<<endl<<"Age:"<<age; } }; template void swap( T &a, T &b) { T temp; temp=a; a=b; b=temp; }

6 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);

7 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(); }

8  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.

9 #include template 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; }

10 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(); }

11  More than one generic type can also be mentioned in the template. template void f1(const T & a, const U & b) { // statements }

12 #include #include #include template T& display(const T & a) {cout<<endl<<"inside single parameter function;";} template T& display(const T& a, const T & b) {cout<<endl<<" inside the 2 parameter function;"; } void main() { clrscr(); display(10); display(12,1); getch(); }

13  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 &); };

14  The classes are similar, other than the type of their data type. template class X { T val; public: void f1( T&); void f2(T&); };

15  Member functions of class templates are defined as follows: template void X ::f1(T & p) { // definition }

16  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.

17  Objects of the template class can be declared as follows X 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 intObj1,intObj2; intObj1.f1(intObj2);

18  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 X {T val1;U val2; //rest of class X }  It can also take a non-type template argument template 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 intObj;

19  The name of the template argument can not be used more than once in the list of template arg. template //ERROR  Te same name can be used in two different template classes template class X { //defn. of class X } template 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 class template class X ; //declaration template class X { //defn. of class X }

20  Formal arguments of the template functions can be objects of the template class template class X { //defn. of class X }; template void f1(X v) { definition of the function }  Nested classes can be created for template classes in the same way as of non-template classes. template class X { class Y { T x; //rest of class Y }; //definition of class X };

21 const int MAX = 10; // stack example Template 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; }

22 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 s1; s1.push(10); s1.push(20);

23 s1.push(30); cout s2; s2.push(3.14); s2.push( 6.28); s2.push( 9.53); cout<<endl<<s2.pop(); complex c1(1.5,2.5),c2(3.5,4.5),c3(-1.5,-2.5); stack s3 s3.push(c1); s3.push(c2); s3.push(c3); cout<<endl<<s3.pop(); }


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

Similar presentations


Ads by Google