Classes & Objects

Course 1 · Ch 4
Classes & Objects
The leap c2-1 explicitly foreshadowed — data and behavior, bundled together for the first time

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

class Circle { public: double radius; double area() { return 3.14159 * radius * radius; } }; Circle c; c.radius = 5.0; std::cout << c.area() << std::endl;

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.

class Account { private: double balance; // inaccessible from outside the class public: void deposit(double amount) { balance += amount; } };

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.

class Circle { public: double radius; Circle(double r) { radius = r; } // runs automatically }; Circle c(5.0); // constructed directly, no separate init call

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.

class Circle { public: ~Circle() { std::cout << "Circle destroyed\n"; } // runs on scope exit };

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.

ConceptC (c2-1/c2-7)C++ class
Data + behaviorseparate — struct, plus functions taking a pointerbundled together directly
Hidden self/object parameterpassed explicitly, by handthis — supplied automatically
Access controlnone — every member always fully exposedpublic/private, compiler-enforced
Initializationa separate, manually-called init function, by conventiona constructor, run automatically
struct and class are otherwise identical in C++
Members, methods, and inheritance all work the same way on both — default access level is the only real difference. Many style guides reserve struct for pure-data types specifically as a signal to readers, purely by convention.
Accessing a private member from outside is a compile error, not a warning
Unlike C, where every struct member is always reachable no matter what, attempting 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

Challenge 1

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 solution
Challenge 2

Add 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 solution
Challenge 3

Explain 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 solution

Chapter 4 Quick Reference

  • A class bundles data and methods together — resolving what c2-1 explicitly 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, replacing c2-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