C++
Review for Test 2: List of Topics Covered
This is a list of topics intended as a checklist to help you recall
what topics have been covered since Test 1, both in lecture class and in
the textbook.
Other Function Topics (continued)
Pass by Reference
- Reference variables
- use & notation when declaring
- an alias for another variable (i.e. nickname)
- useful when variables are in different scopes (i.e. functions)
- used in parameter passing and return values
- Pass by Value - local copies of parameters are made, and copies of
returns are sent back
- Pass by Reference - Copies of parameters and returns are not
made. Local parameters are references to the originals
- Use of
const
with reference parameters to prevent a
function from changing the original (but avoid overhead of making a
copy - faster execution)
Input / Output Libraries
File I/O
- Understand types of files
- Formatted text vs. binary
- sequential vs. random access
- Understand the objects cin and cout, and what
classes they are built from
- library <fstream>
- class ofstream -- for output file streams
- class ifstream -- for input file streams
- Know how to declare file stream objects
- Know how to attach files to stream objects
- member function open()
- Default modes of opening for input files and output files
- Understand the variation -- append mode
- Know how to check to see if the file successfully opened
- member function close() to close a file
- stream can be reused for another file
- Using file streams -- similar syntax as with cout and
cin
- Know the basic syntax for getting a user-entered filename (using
an array of characters)
Other I/O Stream Features
- eof() function
- put() -- for character output
- The two get() functions, for extracting character input
- Understand how each of these are called
- Understand how they differ from reading a char with the extraction
operator >>.
- peek()
- ignore()
- putback()
- Know how to pass stream objects into functions (by reference)
- Understand how to build functions that allow this
- Understand how to call functions that do this
- Understand the relationship between istream and
ifstream, for instance, and which objects can be passed with
which formal parameters. (same for ostream /
ofstream)
Misc -- cctype library
- A useful C library of character handling functions
- toupper(), tolower()
- Understand the boolean functions whose names start with is,
for determining if a character fits in a certain given category
Arrays
Array Properties
- indexed collection of data elements of same type
- consecutive storage locations
- default indexing is 0 through size-1 (where
size is the number of elements in the array)
Declaring Arrays
- format: typeName variableName[size];
- The type can be any basic type or any user-defined type
- the size must be known by the compiler, so it must be a positive
integer literal or constant.
- 2-dimensional arrays
- Example: double table[5][10]
Initializing Arrays
- Can initialize arrays in the same line as declaration
- Format: type name[size] = { list of elements };
- Example: int list[5] = {1, 3, 5, 9, 10};
- The list of elements goes in { } and is separated by commas
- may leave size box empty when initializing on the declaration line
- compiler sets size.
- Can also initialize with for loops (good with regular patterns)
- Special case
- strings: null-terminated character arrays
- can initialize on the declaration with a string literal
- Example: char name[7] = "Marvin";
- size must leave room for null-character '\0'
Using Arrays
- valid indices are 0 through size-1.
- may use any of these index numbers to access a single array element:
- may use any positive integer r-value to index arrays (i.e. variables,
expressions, etc)
- it is the programmer's job to check for out-of-bounds index!
- Copying Arrays
- Assignment between array names does not copy one array to another
- If you want to copy one array to another, do it element by
element (easy with a loop)
Using strings
- A string can be used like a normal array (of characters)
- cout and cin objects also work with strings (for output and input of words)
- >> operator for input stops at white space (space, tab, newline,
etc.)
- only good for one word at a time
- get and getline for reading strings from input
- get, getline read up to specified delimiter -- can
read entire sentences
Arrays as function parameters
Array Usage and Algorithms
- Understand how to handle arrays that are declared to a certain size,
but are not always "full" to their capacity
- Understand common array algorithms and patterns, including (but not
limited to):
- iterating through an array elements with a loop
- Printing array contents
- adding or counting array elements
- finding largest/smallest element of an array
- initializing array contents, with either formulas, user entry, or
reading from a file
- Using parallel arrays
- Swapping or moving around array elements
- Arrays of strings
- Implemented as a 2-D array of type char
- The first dimension indicates how many strings
- The second dimension indicates the allocated size for each
string
- Can refer to the name of one "string" by using the first index
only
Arrays: Searching and Sorting
Searching
- Linear Search
- The most obvious and intuitive search mechanism
- Looks through array elements one by one until the item is located
- takes longer than some specialized search techniques
- Binary Search
- Depends on array being maintained in some sorted order
- Looks at "middle" element, and decide which "side" of the array the
element being searched for would have to be in
- On each comparison, cuts number of possibilities in half
- Runs much faster than linear search
Sorting
- Bubble Sort
- Compares side-by-side elements. If out of order, swap them
- Uses nested loops
- Each run through inner loop "bubbles" one element to the end, its
final position
- Outer loop must run size-1 times
- Selection Sort
- Also uses nested loops
- Run through inner loop "selects" the largest (or smallest) element
of the remaining items (i.e. the ones that are not yet in position),
and swaps it with the end element
- Outer loop must run size-1 times
<cstring> library functions:
- strlen (string length)
- strcpy and strncpy (string copy)
- strcat and strncat (string concatenation)
- strcmp and strncmp (string compare)
string objects
- Built with the string class library
- variable length, flexible
- Supports more intuitive operator notations, like assignment,
comparisons, + for concatenation, etc
- Understand the difference between c-strings and string objects
- Know the commonly used operators, as well as the usage of basic
member functions discussed in class