Exceptions, Classes, and Inheritance
Exceptions, classes, and inheritance interact in a couple ways. First, you can derive one exception class from another, as the standard C++ library does. Second, you can incorporate exceptions into classes by nesting exception class declarations inside a class definition. Third, such nested declarations can be inherited and can serve as base classes themselves.
Listing 15.14 starts us on the way to exploring some of these possibilities. This header file declares a rudimentary Sales
class that holds a year value and an array of 12 monthly sales figures. The LabeledSales
class derives from Sales
and adds a member to hold a label for the data.
// sales.h -- exceptions and inheritance
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.