TEMPLATES in C++ are one of important topics in Object Oriented Programming
208BVijaySunder
119 views
27 slides
May 26, 2024
Slide 1 of 27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
About This Presentation
C++ templates
Size: 556.46 KB
Language: en
Added: May 26, 2024
Slides: 27 pages
Slide Content
UNIT IV Generic Programming with Templates: Introduction, Need for Templates, Definition of class Templates, Normal Function Templates, Over Loading of Template Function, Bubble Sort Using Function Templates Difference between Templates and Macros, Linked list with templates.
Generic Programming Generic programming is an approach where generic types are used as parameters in algorithms so that they work for a variety of specific data types. In C++ generic programming is implemented with “templates”. Templates allow a programmer to write a function or class to work on many different data types with out having to recode specific versions for each data type. Using templates it is possible to create generic functions and generic classes.
Generic Functions A function which works for all types of data is called a Generic function or a function template. A generic function is created using the keyword template. While defining the function template we do not know the data types of parameters and return type also. The data types of parameters and return types are decided while the function is called. The general form of generic function definition is as follows: template <class Ttype > return-type func -name( Ttype v1, Ttype v2,…) { // body of function }
4 Generic Functions C++ routines work on specific types. We often need to write different routines to perform the same operation on different data types. int maximum( int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }
5 Generic Functions float maximum(float a, float b, float c) { float max = a; if (b > max) max = b; if (c > max) max = c; return max; }
6 Generic Functions double maximum(double a, double b, double c) { double max = a; if (b > max) max = b; if (c > max) max = c; return max; } The logic is exactly the same, but the data type is different. Function templates allow the logic to be written once and used for all data types.
7 Generic Functions Generic function to find a maximum value (see maximum example). template <class T> T maximum(T a, T b, T c) { T max = a; if (b > max) max = b; if (c > max) max = c; return max; }
8 Function Templates Usage After a function template is included (or defined), the function can be used by passing parameters of real types. Template <class T> T maximum(T a, T b, T c) … int i1, i2, i3; … Int m = maximum(i1, i2, i3); maximum(i1, i2, i3) will invoke the template function with T as int. The function returns a value of int type.
Normal Function Template
Simple Template Function template <class T> T findMax (T x, T y) { if (x > y) { return x; } else { return y; } } int main() { int x = 0; cout << findMax (x, ++x); // prints 1 cout << x; // prints 1 string s = “hello”; string t = “world”; cout << findMax (s, t); // prints “world” }
Function Template
Overloading of generic functions A generic function can be overloaded. A template function can be overloaded either by a non-template function or using an ordinary function template. The matching of the overloaded function is done in the following manner. a. The ordinary function that has an exact match is called. b. A template function that could be created with an exact match is called. c. If no match is found, an error message is generated.
A Function with multiple generic Types You can define more than one generic data type in the template statement by using a comma-separated list. template < class T1, class T2,.....> return_type function_name (T1 v1, T2 v2,….) { // body of function. }
#include < iostream > using namespace std; template <class T1, class T2> void myfunc (T1 x, T2 y) { cout << x << “ “ << y << “\n”; } int main() { myfunc (1201, “ Bhavani "); myfunc (98.6, ‘A’); }
Bubble sort using Template Function
Bubble sort using Template Function
Generic Classes Like functions ,we can also declare classes to operate on different data types. Such classes are called Generic classes . Before creating an object of the Generic class, the actual type of the data being manipulated must be specified. This is done by specifying the data-type as parameter when the object is created for the template class.
Generic Classes The general form of a generic class declaration is as follows: template <class Ttype > class class -name { //class body } ; Once you have created a generic class, you create a specific instance of that class using the following general form: class-name <type> object_name ;
Difference between Templates and Macros There are 4 main types of preprocessor directives: Macros File Inclusion Conditional Compilation
Preprocessor directives
Macros Macro is a piece of code in a program which is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘# define’ directive is used to define a macro. #define LIMIT 5 //macro definition int main() { for ( int i = 0; i < LIMIT; i ++) { cout << i << "\n"; } }
Macros with arguments We can also pass arguments to macros. Macros defined with arguments works similarly as functions. #define AREA(l, b) (l * b) //// macro with parameters int main() { int a = 10, b = 5, area; area = AREA(a, b); cout << "Area of rectangle is: " << area; }