Review lec4
-
Upload
tamer-abu-alzenat -
Category
Art & Photos
-
view
37 -
download
3
Transcript of Review lec4
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١
Data StructureInstructor :Nabeel Alassaf
Review TemplatesLecture 4
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
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
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
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>
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);
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٧
Output:
6
10
B
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);
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
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
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 ();
};
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;
}
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;
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٤
Output:
10075.44B
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:
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];
}
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;
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١٨
Output:
1003.1416