7 Establishing the Class Invariant
This chapter covers
- The class invariant, an essential view of what a class is and what it does
- Building class behavior from a minimum set of essential functions
- Treating classes as new data types
- Good inheritance design, as seen from the class invariant
- Ensuring that classes work correctly and cooperate with the compiler
Classes represent data types. The C++ standardization committee could never anticipate every data type needed; therefore, the language was designed to be extensible. If a new data type is required, a developer can write it by creating a new class.
While writing a class is a simple process, writing a good class is much more challenging to get correct. Over the years, different advice has been given, and numerous problems have been detected. Eliminating these problems guides a developer into a better class design. Learning some of these problems will help the programmer detect these and similar issues in existing code.
7.1 A Class Invariant Enforces Proper Class Design
A class has at minimum four characteristics that are essential to consider; some academics might suggest more, but these four should be regarded as the minimum:
- Interpretation
- Set of operations
- Range of values
- Memory mechanics