This document discusses templates and exceptions in C++. It covers function templates and how they allow a single function to operate on different data types. It also discusses class templates and how templates are represented in UML diagrams. Finally, it provides an overview of exceptions in C++, including the syntax for throwing and catching exceptions and how control flows when an exception is thrown.
2. Function Templates:
int abs(int n) //absolute value of ints
{ return (n<0) ? -n : n; //if n is negative, return –n }
long abs(long n) //absolute value of longs
{ return (n<0) ? -n : n; }
float abs(float n) //absolute value of floats
{ return (n<0) ? -n : n; }
3. A Simple Function Template:
• Function Template Syntax
• Template argument
• Instantiating
4. Function Templates with Multiple Arguments:
• Template Arguments Must Match
• Syntax Variation
• More Than One Template Argument
• Why Not Macros?
#define abs(n) ( (n<0) ? (-n) : (n) )
6. The UML and Templates:
• Templates (also called
parameterized classes in the
UML)
• Dependencies in the UML
It is a relationship between two
elements such that a change in the
independent one may cause a
change in the dependent one
• Stereotypes in the UML
is a way of specifying additional
detail about a UML element e.g.
<<bind>>
7. Exceptions:
• Exception Syntax
• Sequence of Events:
• Code is executing normally outside
a try block.
• Control enters the try block.
• A statement in the try block causes
an error in a member function.
• The member function throws an
exception.
• Control transfers to the exception
handler (catch block) following the
try block.
• Multiple Exceptions