CS 3358 (Data Structures and Algorithms) by Lee S. Koh
(Taken from Suggested Book's "Chapter 3 / Container Classes" that discusses a version of The Bag Class)

The Invariant of a Class
We've defined the bag data structure, and we have a good intuitive idea of how the structure will be used to represent a bag of items. But as an aid in implementing the class we should also write down an explicit statement of how the data structure is used to represent a bag. In the case of the bag, we need to state how the member variables of the bag class are used to represent a bag of items. There are two rules for our bag implementation:
  1. The number of items in the bag is stored in the member variable used.
  2. For an empty bag, we do not care what is stored in any of data; for a non-empty bag, the items in the bag are stored in data[0] through data[used - 1], and we don't care what is stored in the rest of data.
The rules that dictate how the member variables of a class represent a value (such as a bag of items) are called the invariant of the class. The knowledge of these rules is essential to the correct implementation of the class's functions. With the exception of the constructors, each function depends on the invariant being valid when the function is called. And each function, including the constructors, has a responsibility of ensuring that the invariant is valid when the function finishes. In some sense, the invariant of a class is a condition that is an implicit part of every function's postcondition. And (except for the constructors) it is also an implicit part of every function's precondition. The invariant is not usually written as an explicit part of the preconditions and postconditions because the programmer who uses the class does not need to know about these conditions. But to the implementor of the class, the invariant is indispensable. In other words, the invariant is a critical part of the implementation of a class, but it has no effect on the way the class is used.

The Invariant of a Class
Always make an explicit statement of the rules that dictate how the member variables of a class are used. These rules are called the invariant of the class. All of the functions (except the constructors) can count on the invariant being valid when the function is called. Each function also has the responsibility of ensuring that the invariant is valid when the function finishes.