Classes & Objects
c2-1 named this chapter's whole subject directly: "one way C approximates method-like dispatch without a language feature for it at all." This chapter is that language feature.
From struct to class
c2-1's C struct was pure data — no methods, ever; any function operating on it had to be written separately, taking a pointer as its first argument. A C++ class bundles data and methods together directly, inside the same definition, called with object.method() syntax instead of function(&object).
A First Class
Compare directly against c2-7's own hand-rolled Circle struct with a manually-wired function pointer. Same idea — the language now does the wiring for you.
public and private Access Control
A genuinely new concept: members can be hidden from outside code entirely. A class's members are private by default; a struct's are public by default — the only real difference between the two keywords in C++, worth naming explicitly since it's a common point of confusion.
This is enforced by the compiler — unlike C, where a struct's members are always fully exposed, with zero protection of any kind.
Constructors
A special member function that runs automatically when an object is created — replacing the "manually call an init function" pattern C never even had a formal concept for. Multiple constructors are possible, reusing cpp1-3's own overloading rules directly.
Destructors
A special member function that runs automatically when an object is destroyed — a genuine preview of RAII, this course's own central chapter, coming next.
this — A Hidden Parameter
Inside a member function, this is a pointer to the object the method was called on — exactly the explicit self/void * parameter c2-7 required passing by hand in plain C. The compiler now supplies it invisibly, every call.
| Concept | C (c2-1/c2-7) | C++ class |
|---|---|---|
| Data + behavior | separate — struct, plus functions taking a pointer | bundled together directly |
| Hidden self/object parameter | passed explicitly, by hand | this — supplied automatically |
| Access control | none — every member always fully exposed | public/private, compiler-enforced |
| Initialization | a separate, manually-called init function, by convention | a constructor, run automatically |
struct for pure-data types specifically as a signal to readers, purely by convention.
account.balance from outside Account's own methods genuinely fails to compile — a real, enforced boundary, not a convention the compiler merely suggests.
Coding Challenges
Write a Rectangle class with private width and height members, a constructor taking both values, and a public area() method. Create an instance and print its area.
📄 View solutionAdd a destructor to the Rectangle class from Challenge 1 that prints a message when an instance is destroyed. Create an instance inside a nested scope (an inner {}) and observe when the destructor's message actually prints.
📄 View solutionExplain precisely what the this pointer is standing in for, tying your answer directly back to c2-7's own hand-rolled vtable-style dispatch pattern and what parameter that pattern required the programmer to pass explicitly.
📄 View solutionChapter 4 Quick Reference
- A class bundles data and methods together — resolving what
c2-1explicitly foreshadowed class— private by default;struct— public by default; otherwise identical- A constructor runs automatically on creation; a destructor runs automatically on destruction — RAII's own preview
this— the hidden object pointer, replacingc2-7's explicit self/void* parameter- Private-member access from outside the class is a genuine compile error, compiler-enforced
- Next chapter: Constructors, Destructors & RAII — C++'s own answer to C's manual malloc/free discipline