Templates Templates allows us to create a single

  • Slides: 14
Download presentation
Templates

Templates

Templates allows us to create a single function or class for a group of

Templates allows us to create a single function or class for a group of similar function or class in generic manner. Templates can be categorized into two categories: Function Template: -template declared for function is called function template Class Template: - template declared for class is called class template

Need of template Void sum(int, int) Void sum(float, float) 2 different functions, 2 different

Need of template Void sum(int, int) Void sum(float, float) 2 different functions, 2 different definitions, more code usage To avoid this we use templates

Syntax template<class Type 1> Return_type function_n ame( type 1 a, type 1 b) {

Syntax template<class Type 1> Return_type function_n ame( type 1 a, type 1 b) { ………. //template function body }

Creating Function Templates • Function templates: functions that use variable types – Outline for

Creating Function Templates • Function templates: functions that use variable types – Outline for a group of functions that differ in the types of parameters they use • A group of functions that generates from the same template is often called a family of functions • In a function template, at least one argument is generic, or parameterized • You write a function template and it generates one Object-Oriented or more template functions Programming Using C++, 5 Fourth Edition

Creating Function Templates (continued) • Using the keyword class in the template definition does

Creating Function Templates (continued) • Using the keyword class in the template definition does not necessarily mean that T stands for a programmer-created class type • Many newer C++ compilers allow you to replace class with typename in the template definition 6 Object-Oriented Programming Using C++, Fourth Edition

Creating Function Templates (continued) • When you call a function template, the compiler determines

Creating Function Templates (continued) • When you call a function template, the compiler determines the type of the actual argument passed double amount = -9. 86; amount = reverse(amount); • The designation of the parameterized type is implicit: determined by the compiler’s ability to determine the argument type • The compiler generates code for different functions as it needs, depending on the function calls double reverse(double x) { return -x; Object-Oriented Programming Using C++, 7 Fourth Edition

Syntax template<class Type 1> Return_type function_n ame( type 1 a, type 1 b) {

Syntax template<class Type 1> Return_type function_n ame( type 1 a, type 1 b) { ………. //template function body }

Example #include <iostream. h> #include<conio. h> //using namespace std; template <class type 1> void

Example #include <iostream. h> #include<conio. h> //using namespace std; template <class type 1> void sum(type 1 a, type 1 b) {

cout << a <<"n"<< b <<endl; cout<<a+b<<endl; } int main() { sum(10, 10); sum(10.

cout << a <<"n"<< b <<endl; cout<<a+b<<endl; } int main() { sum(10, 10); sum(10. 5, 11. 5); getch(); return 0; }

Function Template • They allow to create generic functions • They admit any data

Function Template • They allow to create generic functions • They admit any data type as parameters • They return a value without having to overload the function with all the possible data types. • The syntax to declare the function template is as follows: template <class identifier> function_declaration;

Class Template A class can have members based on generic types means their data

Class Template A class can have members based on generic types means their data type is not defined at the time of creation of the class and whose members use these generic types. Syntax: template <class T> Class class_name { //class defination };

Example #include<iostream. h> template <class T> Class mix { T a, b; Public: mix(T

Example #include<iostream. h> template <class T> Class mix { T a, b; Public: mix(T x, Ty) { a=x; b=y; }

T max() { return (a>b? a: b); }; void main { Mix<int> obj(10, 20);

T max() { return (a>b? a: b); }; void main { Mix<int> obj(10, 20); Cout<<“max value is”<<obj. max(); }