Module 2 — Summary¶
The essentials¶
- A class is a type; an object is an instance. The class says what may be done with a book; the object is the book. Two objects of one class hold independent data.
- A class is a set of promises, not a container of data. The data is how the promises happen to be kept today, which is exactly why callers are not allowed to depend on it.
classandstructdiffer only in default member access (privateversuspublic) and default inheritance mode. Usestructfor data with no rules,classfor data with rules.- Write the interface before the representation. State the invariant, write the operations the program needs, and let them tell you which data is required. This order produces no accessors that nothing needs.
privateis a compile-time restriction, not a security feature. It stops callers from naming a member, so you may change storage freely. It does not make a class correct — a private field with a public setter is breakable in one line.thisis a pointer to the object the member function was called on. You need it explicitly in exactly two situations: a parameter name hides a member name, and you want to return the object itself (return *this;) so calls can chain.- A trailing
conston a member function is a promise not to modify the object, checked by the compiler. It is what lets your type be used throughconst T&— and therefore what lets it be passed without copying.const_castis not a fix. - A type split across a header and a source file separates declarations from definitions. Declarations must be visible everywhere the type is used; each definition must exist exactly once. The compiler checks the first, the linker checks the second, and that difference is why link errors arrive so late.
#includeis a paste operation. Two inclusions of the same header in one translation unit mean two class definitions, unless the header has#pragma onceor an include guard.- Objects cooperate by asking, and the rules stay where the data lives. In this
module's
Course/Studentexample, the Student asks and the Course decides, so the two objects cannot disagree about whether a seat was taken. - A class-type parameter is a copy. C++ objects do not have reference semantics:
Book b = a;makes a second book, exactly asint b = a;makes a second integer. Module 10 is where that becomes dangerous and then controlled. - The compiler's generated copy constructor is correct exactly when every member can be copied by value safely — which is not true for a class holding a raw pointer.
The one-sentence version¶
Data and the operations that know the rules about it belong in one unit, and the unit's public surface is a promise the compiler helps you keep.
Terms you must now be able to define¶
Class · object · instance · data member · member function · access specifier ·
interface · implementation · this · const member function · const-correctness ·
header · source file · declaration · definition · scope qualifier · translation unit ·
include guard · aggregate initialisation · copy constructor · invariant
See the glossary. Three of these — declaration, definition and invariant — are load-bearing for the rest of the course. If any of the three is not yet something you can explain to somebody else, write it down as a re-test item and do it before Module 3.
The habit to keep¶
State the invariant, then write the interface, then choose the representation. Every class in the remaining fourteen modules is built in that order, and every design review you ever receive will be, in effect, asking whether you did.
Before you move on¶
- [ ] All warm-up and core exercises attempted, with results checked
- [ ] Lab 2 builds with no warnings, is split across at least three files, and passes its checklist
- [ ] The comparison table is filled in with measured numbers, not estimates
- [ ] The interface table covers every public member of your types, with a promise and a reason for each
- [ ] At least one challenge problem attempted, and its rubric applied honestly
- [ ] Self-assessment score ≥ 7, or the missed questions re-answered
- [ ] The skills checklist on the module page ticked with evidence
- [ ] You have reproduced at least three of the errors in Misconceptions and mistakes in your own code
Further reading¶
- C++ Primer (5th ed.), chapters 7 and 2.6 — class definitions, member functions,
and the
const-correctness this module is built on. The most careful treatment of the syntax at this level. - Effective C++ (3rd ed.), Items 18–22 — interface design: why making interfaces easy to use correctly and hard to use incorrectly is the whole job of a class author. Written for a reader who has just finished this module.
- A Tour of C++ (3rd ed.), chapter 2 — classes, the same ideas compressed, useful for a second pass in a week.
- The C++ Core Guidelines — the "Classes" section:
C.2(useclassif there is an invariant),C.9(minimise the exposure of members), andC.20(avoid defining a class if you can use a plainstruct). About twenty short rules, and this module is roughly their content.
See Further Reading for editions and why these are the right ones.
Next: Module 3 — Constructors and Destructors. Module 3 takes the one thing this module left alone: what happens between an object existing and not existing. It also answers the question Example 4 raised and did not answer — when exactly a copy is made, and when it is destroyed.