A class defines a new C++ data type that combines representation and operations. Its designer determines the meaning, valid states, and permitted interactions, so flexibility brings responsibility. section 1.4.1 section 7.2.1
Designing a useful class means treating it as a type rather than merely grouping code:
- Protect the class invariant: constructors and member functions should preserve valid, unsurprising object state throughout its lifetime. section 7.2.1
- Choose operations that clients can use intuitively, including behavior for construction, copying, assignment, collections, and inheritance when relevant. section 7.4
- State special-member intent explicitly: use =default for compiler-generated behavior and =delete when an operation must not be available. section 2.4
Mistake: exposing an invalid data type
A naïve Rational class stores numerator and denominator as doubles, provides unrestricted setters, and allows a zero denominator. That interface makes invalid states possible and forces callers to understand representation details. A safer design stores integers, validates the denominator in construction, reduces values internally, and exposes intuitive operators such as operator+ and operator*.




















