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= "<<a<<" b= "<<b; } Swap(1,2);// Generic type is int Swap(2.5,4.6);// Generic type is float Swap(‘a’,’b’);// Generic type is char swap("popo","peepe");// Generic type is string

6 6 Generic Functions template T square( T one) {return(one*one);} main() { cout<<“square of integer”<<square(2);// Generic type is int cout<<“square of float”<<square(2.5);// Generic type is float }

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<<x<<y;} Since the first argument is a generic type, can be used to display any type of data Second argument must be integer Show(“popo”,3);// first is any type sec must be int Show(2,5);// first is any type sec must be int Show(‘A’,8);// first is any type sec must be int

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<<x;}// template fun void show(int x){cout<<x;}// ordinary fun main() { show(10);//o rdinary fun will call (10 is int exact match) show(2.4);//template fun will call show(‘A’);//template fun will call }

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= "<<a<<" b= "<<b; } }; void main() { A obj; obj.swap(1,2); }

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<<a<<b;} main() { example obj1(1,2);// first arg is int and sec is int example obj2(4.6,2);// first arg is float and sec is int example obj2(4.6,2);// first arg is char and sec is float }

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"<<x<<" is pushed..\n"; }

17 17 Stack implementation using Template void pop() { if(st==0) cout<<"\n EMPTY STACK...\n"; else { st--; cout<<"\n"<<stack[st]<<" is poped..\n"; } void show() {if(st==0) cout<<"\n EMPTY STACK..\n"; else {for(int i=0;i<st;i++) { cout<<stack[i]<<" "; } };

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