Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.

Similar presentations


Presentation on theme: "Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar."— Presentation transcript:

1 Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar in passing value/variable into function. Instead of passing a variable, we pass a type (such as int, double, and Point) into template. Passing type is known as generic programming, as we can program in generic type and invoke the code using a specific type.

2 The goal of generic programming is to write code that is independent of the data types.
In C language, all codes are tied to a specific data type. For container data structures (such as array and structure), you need to specify the type of the elements. For algorithms (such as searching or sorting), the code works only for a specific type, you need to rewrite the code for another type. Can we write a single sorting routine that works on all types (or most of the types) by specifying the type during invocation? Can we have a general container that can work on all types?

3 template lets you program on generic type, instead of on a specific type.
Template supports so-called parameterized type - i.e., you can use type as argument in building a class or a function (in class template or function template). Template is extremely useful if a particular algorithm is to be applied to a variety of types, e.g., a container class which contains elements, possibly of various types.

4 A template can be used to create a family of classes and functions.
For example , a class template for an array class would enable us to create array of various type such as int array , float array and double array . Similarly ,we can define a template for a function like “mul()” that can help us to create various version of “mul()” for mltiplying “int” , “float” or “double” type value. Templates are also called “parameterized classes or function”

5 template <typename T>
void mySwap(T &a, T &b) { T temp; temp = a; a = b; b = temp; } #include <iostream> using namespace std; template <typename T> void mySwap(T &a, T &b); int main() { int i1 = 1, i2 = 2; mySwap(i1, i2); // Compiler generates mySwap(int &, int &) cout << "i1 is " << i1 << ", i2 is " << i2 << endl; char c1 = 'a', c2 = 'b'; mySwap(c1, c2); // Compiler generates mySwap(char &, char &) cout << "c1 is " << c1 << ", c2 is " << c2 << endl; double d1 = 1.1, d2 = 2.2; mySwap(d1, d2); // Compiler generates mySwap(double &, double &) cout << "d1 is " << d1 << ", d2 is " << d2 << endl; }

6 void mySwap(int &a, int &b)
take note that the C++ compiler generate a version of the code for each type used in the program, in a process known as instantiation of template. For example, with type of int, the following code will be generated by the compiler: void mySwap(int &a, int &b) { int temp; temp = a; a = b; b = temp; }

7 /* Test function Template */
#include <iostream> using namespace std; template<typename T> T abs(T value) { T result; // result's type is also T result = (value >= 0) ? value : value; return result; } int main() { int i = 5; cout << abs(i) << endl; double d = 55.5; cout << abs(d) << endl; float f = 555.5; cout << abs(f) << endl; Note:The compiler creates three versions of the  abs function based on the function template, for types of int, double, and float.

8 #include <iostream> using namespace std;
/* C++ program to display larger number among two numbers using function templates. */ /* If two characters are passed to function template, character with larger ASCII value is displayed. */ #include <iostream> using namespace std; template <typename T> T Large(T n1, T n2) { return (n1>n2) ? n1:n2; } Void main() { int i1, i2; float f1, f2; char c1, c2; cout<<"Enter two integers: "; cin>>i1>>i2; cout<<Large(i1, i2)<<" is larger."; cout<<"\n\nEnter two floating-point numbers: "; cin>>f1>>f2; cout<<Large(f1, f2)<<" is larger."; cout<<"\n\nEnter two characters: "; cin>>c1>>c2; cout<<Large(c1, c2)<<" has larger ASCII value.";

9 The following is the example of a function template that returns the maximum of two values:
#include <iostream> #include <string> using namespace std; template <typename T> inline T const& Max (T const& a, T const& b) { return a < b ? b:a; } int main () int i = 39; int j = 20; cout << "Max(i, j): " << Max(i, j) << endl; double f1 = 13.5; double f2 = 20.7; cout << "Max(f1, f2): " << Max(f1, f2) << endl; string s1 = "Hello"; string s2 = "World"; cout << "Max(s1, s2): " << Max(s1, s2) << endl; return 0; }

10 Class Templete Templates allows us to define generic class.
It is a simple perocess to create a geeric class using with an anonymous class. The general format of the class t emplate<class T> class classnam { //class member specification // with anonyoums type T // whenever appropriate };

11 # include<iostream.h>
Using namespace std Template <class T1 , class T2> class test { T1 a ; T2 b; Public : test ( T1 x, T2 y) { a=x; b=y; } Void show() cout << a << “and “<< b << “\n”; }; void main() { Test <float,int> test1 (1.23, 123); T est< int , char> test2(100,’W’); test1.show(); test2.show(); } Output: 1.23 and 123 100 and W


Download ppt "Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar."

Similar presentations


Ads by Google