Chapter 17: Advanced C++ Topics

Chapter Goals

Operator Overloading

Operator Overloading (Syntax 17.1 : Overloading Operator Definition)

Syntax 17.1 : Overloading Operator Definition

return_type operatoroperator_symbol(parameters)
{
   statements
}
Example:
int operator-(Time a, Time b)
{
   return a.seconds_from(b)
}
Purpose: Supply the implementation of an overloaded operator.

Operator Overloading

Overloading Operators

Overloading Operators

Operator Overloading

Operator Overloading (overload.cpp)

Operator Overloading

Operator Overloading

Operator Overloading

Automatic Memory Management

Automatic Memory Management

Automatic Memory Management (Syntax 17.2 : Destructor Definition)

Syntax 17.2 : Destructor Definition

Class_name::~Class_name()
{
   statements
}
Example:
Department::~Department()
{
   delete receptionist;
}
Purpose: Supply the implementation of a destructor that is invoked whenever and object goes out of scope.

Automatic Memory Management

Automatic Memory Management

Automatic Memory Management

Automatic Memory Management

Automatic Memory Management

Automatic Memory Management

Automatic Memory Management

Automatic Memory Management (department.cpp)

Memory Management for Linked Lists

Memory Management for Linked Lists

Templates

Templates (Syntax 17.3 Template Class Definition)

Syntax 17.3 : Template Class Definition

template<typename type_variable>
class class_name
{
   features
};
Example:
template<typename T>
class Pair
{
public:
   Pair(T a, T b);
   T get_first() const;
   T get_second() const;
private:
   T first;
   T second;
};
Purpose: Define a class template with a type parameter.

Templates

Templates (Syntax 17.4 Template Member Function Definition)

Syntax 17.4 : Template Member Function Definition

template<typename type_variable>
return_type class_name<type_variable>::function_name(parameters) constopt
{
   statements
}
Example:
template<typename T>
T Pair<T>::get_first() const
{
   return first;
}
Purpose: Supply the implementation of a member function for a class template.

Templates

Templates (list.cpp)

Nested Classes and Name Spaces

Nested Classes and Name Spaces (Syntax 17.5 : Nested Class Declaration)

Syntax 17.5 : Nested Class Declaration

class Outer_class_name
{
   ...
   class Nested_class_name
   {
      ...
   };
   ...
}
Example:
class List
{
   ...
   class Iterator
   {
      ...
   };
};
Purpose: Define a class whose scope is contained in the scope of another class.

Nested Classes and Name Spaces

Nested Classes and Name Spaces

Nested Classes and Name Spaces (Syntax 17.6 : Name Space Definition)

Syntax 17.6 : Name Space Definition

namespace name_space_name
{
   feature1
   feature2
   ...
   featuren
};
Example:
namespace ACME_Software_San_Jose_CA_US
{
   class map
   {
      ...
   };
}
Purpose: Include a class, function or variable in a name space.

Nested Classes and Name Spaces

Nested Classes and Name Spaces (Syntax 17.7 : Name Space Alias)

Syntax 17.7 : Name Space Alias

namespace alias_name = name_space_name;
Example:
namespace acme = ACME_Software_San_Jose_CA_US;
Purpose: Introduce a short alias for the long name of a name space.

Exception Handling

Exception Handling

Exception Handling (Syntax 17.8 : Throwing an Exception)

Syntax 17.8 : Throwing an Exception

throw expression;
Example:
throw logic_error("illegal future_value parameter");
Purpose: Abandon this function and throw a value to an exception handler.

Exception Handling

Exception Handling (Syntax 17.9 : Try Block)

Syntax 17.9 : Try Block

try
{
   statements 
}
catch (type_name1 variable_name1)
{
   statements
}
catch (type_name2 variable_name2)
{
   statements
}
...
catch (type_namen variable_namen)
{
   statements
}
Example:
try
{
   List staff = read_list();
   process_list(staff);
}
catch(logic_error& e)
{
   cout << "Processing error " << e.what() << "\n";
}
Purpose: Provide one or more handlers for types of exceptions that may be thrown when executing a block of statements.

Exception Handling

Exception Handling

Exception Handling

Exception Handling

Exception Handling

Exception Handling

Exception Handling

Exception Handling

Exception Handling (Syntax 17.10 : Exception Specification)

Syntax 17.10 : Exception Specification

return_type function_name(parameters)
   throw (type_name1, type_name2, ..., type_namen)
Example:
void process_products(fstrream& fs)
   throw(UnexpectedEndOfFile, bad_alloc)
Purpose: List the types of all exceptions that a function can throw.