Core · Module 5
Classes, Construction and RAII
Design classes that always start valid and clean up after themselves, with correct copy, move and destruction.
A struct groups data, but it cannot promise the data starts valid or ends cleanly. This module turns structs into classes that own resources: files, heap blocks and locks. Its traces show constructors acquiring memory and destructors releasing it, so by the end your library-management types manage themselves with no manual new or delete in user code.
- 1Classes and Access Control: Building Your First TypePro — members onlyTurn a struct into a class that guards its own invariants, with private data, public operations and a constructor that leaves every object valid.10 min
- 2Constructors, Destructors and the Rule of ZeroPro — members onlyGive every Contact a valid start with member-initializer lists, delegating constructors, and deleted operations, then let the Rule of Zero remove ownership work.10 min
- 3Copy Semantics: Deep Copies and the Rule of ThreePro — members onlyFix a leaking buffer class with a deep copy constructor, copy assignment, and destructor, and learn when the Rule of Three demands all three.11 min
- 4Smart Pointers: unique_ptr, shared_ptr and weak_ptrPro — members onlyReplace raw owning pointers with unique ownership, shared ownership, and non-owning observers, using make_unique and make_shared throughout.10 min
- 5Friend Functions, Namespaces and Operator OverloadingPro — members onlyGrant narrow access with friends, organize code with namespaces, and overload plus and increment with const-correct, idiomatic signatures.11 min
- 6Project: A Safe Contact Class With No LeaksPro — members onlyAssemble constructors, deep-copy control, smart pointers, friends, and namespaces into one Contact that owns safely and prints cleanly.12 min