CHAPTER 4 ExceptionHandling Templates
CHAPTER 4 ExceptionHandling Templates
• Programs can
– Recover from exceptions
– Hide exceptions
– Pass exceptions up the “chain of command”
– Ignore certain exceptions and let someone else
handle them
• An exception is a class
• Usually derived from one of the system’s exception
base classes
• If an exceptional or error situation occurs,
program throws an object of that class
• Object crawls up the call stack
11
12
– Function Overloading
– Function Template
FunctionTemplate
Template < TemplateParamList
>
FunctionDefinition
TemplateParamDeclaration: placeholder
class typeIdentifier
typename variableIdentifier
TemplateFunction Call
Template Functions
One Function Definition (a function template)
Compiler Generates Individual Functions
Class Template
Template < TemplateParamList
>
ClassDefinition
TemplateParamDeclaration: placeholder
class typeIdentifier
typename variableIdentifier
class GList_int
{
public: int
private: int
int length;
ItemType data[MAX_LENGTH];
};
int
vector is a class that creates a dynamic array allowing insertions and deletions at the
vector <vector> Random access
back.
list list is the sequence containers that allow the insertions and deletions from anywhere. <list> Bidirectional
deque is the double ended queue that allows the insertion and deletion from both the
deque <deque> Random access
ends.
set set is an associate container for storing unique sets. <set> Bidirectional
multiset Multiset is an associate container for storing non- unique sets. <set> Bidirectional
Map is an associate container for storing unique key-value pairs, i.e. each key is
map <map> Bidirectional
associated with only one value(one to one mapping).
multimap is an associate container for storing key- value pair, and each key can be
multimap <map> Bidirectional
associated with more than one value.
37
38
39
40
41
int data[100];
...
int * where = find(data, data+100, 7);
43
44
class randomInteger {
public:
unsigned int operator () (unsigned int max) {
// compute rand value between 0 and max
unsigned int rval = rand();
return rval % max;
}
};
45
class LargerThan {
public:
// constructor
LargerThan (int v) { val = v; }
// the function call operator
bool operator () (int test)
{ return test > val; }
private:
int val;
};
list<int>::iterator found =
find_if (aList.begin(), aList.end(), LargerThan(12));
47