CS107 Study Guide

Unit 5: Advanced Concepts

5a. Write class and function templates

  • What is a template?
  • How are templates used?
  • What is the syntax for invoking a template?

Templates allow you to write rules so that functions and classes can operate on different data types without having to be rewritten for each separate case. So, for example, rather than overloading several different functions, it is possible to create a template that can take various cases into account. Then, at compile-time, the code associated with the template rules for the specific case at hand is inserted into the program. As an example, consider the following code snippet:

template <typename T>
void Print(T printval)
{
  cout << printval << "\n";
}
int main() {
  Print<int>(3);
  Prin<float>(3.3);
  Print<string>("three");
}

The template instruction indicates that some yet-to-be specified data type T will be used as an input to the Print function. The true data type will not be known until compile-time. At that point, the code in main will resolve the data type based upon the function call and the function code will be generated. Some loosely refer to this process as 'on-demand compilation'.

You can use any name you like for the typename (T and S are often chosen). In addition,

template <class T>

is also acceptable syntax in the above example. Similar syntax holds for template use within. Once template <typename T> is invoked, you can use T as a variable type to declare any data attribute within the class. Again, details about the usage are worked out at compile-time based upon declarations within the class definition and the class method call. Finally, there may be cases where a template must be specialized for a given data type (even though many uses are still possible). Template specialization is indicated using the syntax template <>.

For a review see C++ Templates and Introduction to C++ Templates.


5b. Code with a class that manipulates files

  • What are the basic steps required for handling a file?
  • What methods accomplish these basic steps?
  • What is the syntax for reading from and writing to a file?

Part of the goal of file handling syntax is to make standard input/output similar to file input/output. This can be accomplished using

#include <fstream>

which will include the file handling library for both reading (input) and writing (output). Alternatively, the ofstream library can be used for file output and ifstream can be used for file input. Once this is done, the syntax for file I/O will appear quite similar to using cin and cout from the standard library.

There are three basic steps to handling a file: open, perform operations, and close. Opening a file makes a request to the operating system so that the file can be found. In addition, the operation to be performed (e.g. "r" implies reading, "w" implies writing) must be specified. The desired operations are then performed while the file is open. Lastly, the file must be closed. Not surprisingly, the methods involved in opening and closing files are named open and close. For the operations to read or write the data, similar in syntax and usage to printf and sprintf is the command fprintf. The fscanf command as an extension of the scanf command should also be reviewed. Lastly, it is possible to invoke file objects using ios::mode where modes such as trunc, in, out, app, etc should be reviewed.

To review, see Reading File Input in C++, Output File Streams in C++, and Input and Output.


5c. Use exceptions in C++ code

  • What is an exception?
  • How are exceptions handled?
  • What are some common exception types?

During the execution of a program, there is potential for unexpected runtime errors and abnormalities that can interrupt normal program flow (i.e. exceptions). Exception handling is the process of dealing with such program interruptions in order to allow for continued execution in light of some abnormality or to allow for a nonvolatile program termination. The exception is said to be thrown at the place in the program where the exception occurs. To anticipate the exception to be thrown, an exception handler should be written to deal with the condition that has caused the exception.

At the basic level, three keywords used to handle exceptions are try, catch and throw. The try block is a block of code which could cause an exception. An exception is thrown by using the throw keyword from inside the try block. The try block is immediately followed by one or many catch blocks each designed to catch and then handle an exception thrown by the try block.Exception types such as range_error, overflow_error, underflow_error, bad_alloc, length_error, and domain_error from the standard library should be reviewed.

To review, see Exception Handling.


Unit 5 Vocabulary

This vocabulary list includes terms that students need to know to successfully complete the final exam for the course.

  • catch
  • close
  • exceptions
  • file input/output
  • open
  • reading
  • template
  • thrown
  • try
  • writing