Review lec4

18
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١ Data Structure Instructor :Nabeel Alassaf Review Templates Lecture 4

Transcript of Review lec4

Page 1: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١

Data StructureInstructor :Nabeel Alassaf

Review TemplatesLecture 4

Page 2: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢

Templates

• Templates: a single code body for a set of related functions (called function template) and related classes (called class template)

• The syntax for templates is:template <class Type>

declarationwhere Type is the type of the data and declaration is either a function declaration or a class declaration

Page 3: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٣

Templates (continued)

• template is a reserved word

• The word class in the heading refers to any user-defined type or built-in type

• Type is called a formal parameter to the template

• Just as variables are parameters to functions− Data types are parameters to templates

Page 4: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٤

Function Templates

• The syntax of the function template is:template <class Type>

function definitionwhere Type is called a formal parameter of the

template• Type

− Specifies type of parameters to the function − Specifies return type of the function− Declares variables within the function

Page 5: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٥

Example:

// function template#include <iostream>using namespace std;

template <class T> T GetMax (T a, T b);int main () {int i=5, j=6, k;long l=10, m=5, n;char X='A',Y='B',C;

è Or use template <typename T>

Page 6: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٦

k=GetMax<int>(i,j); //k=GetMax(i,j);n=GetMax<long>(l,m); //n=GetMax(l,m);C=GetMax<char>(X,Y); //C=GetMax(X,Y);cout << k << endl;cout << n << endl;cout << C << endl;

return 0;}

template <class T>

T GetMax (T a, T b) {

T result;

result = (a>b)? a : b;

return (result);

}

Page 7: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٧

Output:

6

10

B

Page 8: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٨

note :We can also define function templates that accept more than one type parameter, simply by specifying more template parameters. For example:

template <class T, class U>

T GetMin (T a, U b)

{ return (a<b?a:b); }

In main use:

int i,j;

long l;

i = GetMin<int,long> (j,l);

Or simply use:

i = GetMin (j,l);

Page 9: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٩

Class Templates

• Class templates: a single code segment represents a set of related classes

• Syntax:template <class Type>class declaration

• Called parameterized types− A specific class is made based on the

parameter type

Page 10: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٠

Class Templates (continued)

• A template instantiation can be created with either a built-in or user-defined type

• The function members of a class template are considered function templates

Page 11: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١١

Example:

// class template headertemplate <typename T>class pair{

private:T a,b;

public:pair (T first, T second){a=first; b=second;}T getmax ();

};

Page 12: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٢

template <typename T>T pair<T>::getmax (){T retval;retval = (a>b? a : b);return retval;

}

Page 13: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٣

//main user file#include <iostream>#include "CTdef.h";using namespace std;

int main (){pair <int> object (100, 75);pair <double> object1 (15.4, 75.44);pair <char> object2 ('A', 'B');cout << object.getmax()<<endl;cout << object1.getmax()<<endl;cout << object2.getmax()<<endl;return 0;

}

Page 14: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٤

Output:

10075.44B

Page 15: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٥

#include <iostream>using namespace std;

template <class T, int N>class sequence{

private:

T memblock [N];public:void setmember (int x, T value);T getmember (int x);

};

Example:

Page 16: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٦

template <class T, int N>void sequence<T,N>::setmember (int x, T value){memblock[x]=value;

}

template <class T, int N>T sequence<T,N>::getmember (int x){return memblock[x];

}

Page 17: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٧

int main () {sequence <int,5> myints;sequence <double,5> myfloats;myints.setmember (0,100);myfloats.setmember (3,3.1416);cout << myints.getmember(0) << '\n';cout << myfloats.getmember(3) << '\n';return 0;

}

Page 18: Review lec4

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٨

Output:

1003.1416