We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Const Correctness and Class Design step 3 of 3
Keep ledger entries and their total consistent
This Ledger stores both its entries and a cached total. After recording values, the total should equal the sum of the entries. That relationship is an invariant: a condition its supported operations should preserve.
The starter lets callers change the entries without updating the total:
std::vector<int>& entries() { return entries_; }
Although entries_ is private, the returned mutable reference lets callers append, erase or replace values. For example, appending 5 leaves the vector containing 5 while total_ is still zero. Making the member private does not protect this relationship when the public accessor allows unrestricted writes.
Offer a read-only view and a controlled update
Use this accessor for reading:
const std::vector<int>& entries() const { return entries_; }
The const in the return type prevents ordinary mutation through that reference. The trailing const makes the function callable on a const Ledger. This does not freeze the original object: ledger.add(...) can still change it, and readers of the returned reference see the updated vector. The reference is borrowed, so it must not be used after the Ledger is destroyed.
This interface still exposes std::vector as the representation. A smaller query interface or a returned snapshot might be preferable in another design. Returning const T& is not a universal rule for every getter; small values are often better returned by value, and borrowing introduces lifetime obligations.
For updates, give callers the operation they need:
void add(int value);
It appends an entry and updates the cached total in one place. Append first: push_back may allocate and throw. For this vector<int>, a failed append leaves the vector unchanged, so updating total_ only after success keeps the two in agreement. The exercise inputs keep the subsequent integer addition in range.
Encapsulation makes this update easier to maintain, but does not prove all future operations correct. Copying, moving, new mutators and error paths still need review when a class has related fields. A class and a struct can both have invariants; their C++ language differences concern default access and inheritance.
Your task
Update Ledger and record as follows:
- Replace the mutable entries accessor with the const-reference accessor above. Do not keep a public mutable overload.
- Add void add(int value), which appends value and updates total_. Keep both data members private and retain the existing total() and count() accessors.
- Have record call add for every input value greater than or equal to floor.
Preserve accepted values in their input order, including duplicates and zero. Negative values are accepted when they meet the floor. Leave the input vector unchanged. record returns the number added by that call, not the Ledger's lifetime count. Repeated calls append to the existing Ledger.
For values [5, 1, 7] and floor = 3, the accepted entries are [5, 7],
record returns 2, and total() is 12. Equality counts: value 7 is accepted
when floor is 7. Empty input or no accepted values leaves the Ledger unchanged.
Inputs, total counts and every running sum fit int. The exercise calls record with input storage separate from the Ledger's entries and does not move Ledgers.
What the harness reports
Starting with an empty Ledger, it returns [added, count, total, real_sum],
where real_sum is computed by iterating over entries(). The example produces
[2, 2, 12, 12]. Empty input produces [0, 0, 0, 0].
The last two values must agree, but that equality alone does not prove the accessor is read-only. Keep the required signature as well as the output behavior. The starter updates the vector directly and never updates the cached total.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
C++ reference solution
Sign in to track your attempts toward unlocking the reference solution.