C++ Review: List of Topics (since Test 2)
Templates
Function Templates:
-
functions that can work with multiple parameter types (as seen in chapter
3)
-
compiler builds a separate function for each needed type, based on the
calls used
-
an easy way to create overloaded functions without writing individual versions
-
Operations performed inside the function need to be valid for types used
in the calls
Class Templates:
-
Extension of function template idea.
-
Allows creation of a "generic" class, where the type of item stored can
vary
-
Again, operations used inside the class need to be valid for any type used
to instantiate the class
-
Each member function written as a function template
Template Declarations and Object Instantiations:
Data Structures
Basic Data Structures
-
created to store data in useful ways, often beyond the built-in mechanisms
(like arrays)
Some Basic Data Structure Types
-
Stack - First In Last Out (FILO) structure.
- push -- function to insert data into stack
- pop -- function to delete data item from stack
- Inserts and deletes always from the same end
-
Queue - First In First Out structure. Similar to waiting in line.
- enqueue -- function to insert data into queue
- dequeue -- function to delete data item from queue
- Inserts and deletes always from the opposite ends
-
Vector - Storage of a list using array-based storage internally
-
Linked List - Storage of a list in a linear format using self-referential
objects.
-
A self-referential object contains data, along with one or more pointers,
which point to other objects of the same type
-
Each node of a linked list stores a piece of data, and points to
the next node in the list.
Implementing Basic Data Structures
-
encapsulation of data structure inside an object means details can be handled
internally, outside access through simpler interface
-
often involves pointers and dynamic memory allocation (inside the data
structure).
-
Templates are commonly used, to make data structures more general, useful
with more than one type of data.
-
Some structures can be implemented with others, through inheritance or
composition.
-
A stack can be implemented with a linked list or a vector, for example
Recursion
- Recursive function -- a function that calls itself
- Involves extra activation records on function call stack
- recursion vs. iteration
- Some example algorithms looked at: Factorial, Fibonnaci, GCD,
sorting, binary search
- Recursion sometimes easier way to write a function
Exception Handling
Exception Handling:
-
A method of error-handling
-
good for processing errors that must be handled in places other than where
they occurred
-
good for handling errors from widely used libraries or other components
-
should not be used for general program control (confusing to the reader)
Basic Syntax:
-
uses reserved words: try, throw, catch.
-
try
-
label used for a block that encloses an area where exceptions might be
thrown
-
try { }
-
throw
-
catch
-
The catch blocks immediately follow the try blocks. Can have more
than one.
-
Each catch can take one parameter, indicating the type of exception
to be caught
-
Special catch block: catch(...) { }
- Will catch any thrown exception. Useful for a default case - a
"catch-all".
Miscellaneous
Conditional Compilation
-
#define SYMBOL
-
This simply brings the symbol into existence.
-
#ifdef - (if defined)
-
#ifndef - (if not defined)
-
#endif
Can be used like if statements, at the compiler level.
Used to check if symbols are defined, and compile sections of code
based on the result.
#endif is the ending of the "body" of the if-statement when
using #ifndef or #ifdef
Example
#ifndef _FRACTION_H
#define _FRACTION_H
...
// conditionally compiled code goes here
...
#endif