Unit 5: Advanced Concepts
In this unit, we will first explore the concept of generic programming, and how you can harness the power of templates to make classes and functions more reusable and adaptable to your personal needs. Then, we will explore ways to add content to our program by reading from files and then storing the data by writing to output files. Finally, we will explore the use of exceptions in C++. Exceptions allow us to anticipate problems that might occur in code and handle this problems through the use of exceptions, telling the program specifically how it is to behave when these problems occur.
Completing this unit should take you approximately 8 hours.
Upon successful completion of this unit, you will be able to:
- write class and function templates;
- code with a class that manipulates files; and
- use exceptions in C++ code.
5.1: Writing Class and Function Templates
Read this article, which discusses generic programming and the use of templates.
Watch this video to learn more about templates.
5.2: Inputting and Outputting with Files
The ability to read from a file is a critical task that allows for batch processing. Watch this video to learn how you can read data from a file, so that data input doesn't always have to come from the keyboard.
Sometimes once you have finished processing your data, you want to save the data so that it can be re-input at a later time as an input file. Watch this video to learn how to write to an output file using output file streams.
Read this article and complete the exercises at the end to test your skills in inputting from and outputting to files.
5.3: Exception Handling
When things go wrong in programming, as they usually do, the last thing you want is for the program to crash or perform some erroneous actions. Using exception handling allows the programmer to control what happens when the unexpected happens.
Unit 5 Exercises
Complete these activities to practice reading from and writing to files.