Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.

Similar presentations


Presentation on theme: "Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function."— Presentation transcript:

1 Template

2 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function or class operates is specified as a parameter. Thus using one function or class with several different types. Inheritance provide a way to re-use object code Templates provide a way to re-use source code

3 3 Generic Functions A generic function defines a general set of operations that will be applied to various types of data. A single general procedure can be applied to a wide range of data. Based on the argument types provided in the function call.

4 4 Generic Functions compiler will automatically replace the actual data type when it creates a specific version of the function. The keyword class to specify a generic type in a template declaration template template ret-type func-name(parameter list) { // body of function }

5 5 Generic Functions template void swap(T a, T b) { T temp=a; a=b; b=temp; cout<<"a= "<

6 6 Generic Functions template T square( T one) {return(one*one);} main() { cout<<“square of integer”<

7 7 Generic Functions A Function with Two Generic Types we can define more than one generic data type in the template statement by using a comma-separated list. template void myfunc(type1 x, type2 y) { cout << x << ' ' << y << '\n'; } main() { myfunc(10, "I like C++");// first is int sec is string myfunc(98.6, ‘a’);// first is float sec is char }

8 8 Generic Functions Function template with multiple parameters we can mix standard parameters with generic type parameters in a template function. These nongeneric parameters work just like they do with any other fun Template Void show( T x,int y ) {cout<

9 9 Generic Functions Overloading of template fun Template fun can overloaded either by template funs or by ordinary funs of its name Calling ordinary fun, that has an exact match Calling template fun that could be created with exact match An error occurs if no match is found

10 10 Generic Functions Overloading of template fun template void show(T x){cout<

11 11 Generic Class Generic classes are useful when a class uses logic that can be generalized. Consider a class is created with 3 integer variables and 1 fun to find biggest among 3 ints Suppose we want to perform same above operation with float, solution is to change the data type of integers to float Using template class we can solve it to perform same operations for any type of data

12 12 Generic Class template class A { T a,b; public: void swap(T x, T y) { a=x; b=y; T temp=a; a=b; b=temp; cout<<"a= "<

13 13 Generic Class A obj; The object obj works like same as the of the class in which a and b are int type A obj; The object obj works like same as the of the class in which a and b are char type

14 14 Generic Class Class template with multiple parameters Template Class example { T1 a; T2 b; Public: example(T1 x,T2 y){ a=x;b=y;} void show(){cout<

15 15 Template Restrictions Restrict the Types of Template Argument for a Template Class Torestrict the type of template arguments for a template class by using the Forward template declaration. Suppose a template class, want to use it only for types int : Then in object creation class object; Eg A obj; specify the type is int

16 16 Stack implementation using Template template class S { T stack[10]; int max,st;a public: S(){max=3;st=0;} void push(T x) { if(st==max) cout<<"\n STACK ID FULL..\n"; else { stack[st]=x; st++; cout<<"\n"<

17 17 Stack implementation using Template void pop() { if(st==0) cout<<"\n EMPTY STACK...\n"; else { st--; cout<<"\n"<

18 18 Stack implementation using Template void main() {S obj; int c,data; do {cout<<"\nSTACK\n1) PUSH\n2) POP\n3) DISPLAY"; cout >c; switch(c) {case 1: cout >data; obj.push(data);break; case 2: obj.pop(); break; case 3: obj.show(); break; case 4:break; default: cout<<"invalid choice"; break; } while(c!=4); }


Download ppt "Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function."

Similar presentations


Ads by Google