5.2.5 · D1C++ Programming

Foundations — Classes — member functions, access specifiers (public, private, protected)

1,647 words7 min readBack to topic

Before you can read the parent note fluently, you must be able to look at each mark on the page and instantly know what it means, what it looks like, and why the topic can't live without it. We build them in the exact order they depend on each other — nothing is used before it is earned.


0. The starting point: a variable and a function

Everything here rests on two things you already meet on day one of C++.

The whole topic is the answer to one question: what if we glued a set of boxes and a set of machines together into a single sealed unit? That unit is the class.

Figure — Classes — member functions, access specifiers (public, private, protected)

1. The dot . — reaching into an object

Before classes even, you use the dot with strings and streams. It is the very first symbol of the topic.

Why the topic needs it: once data and functions live inside an object, you need a way to say which object and which member. acc.deposit(100) reads as "on the box named acc, press the button deposit." Without the dot there is no way to name a member.


2. class and object — blueprint vs thing

Why both are needed: the blueprint is written once; you can stamp out millions of objects from it. Each object gets its own copy of the data boxes but they share the one copy of the machine code.

Figure — Classes — member functions, access specifiers (public, private, protected)

3. public / private / protected — the three walls

Now the boxes are inside the object. Who is allowed to open them?

The picture: a box with three kinds of walls — an open counter (public), a locked interior (private), and a repair hatch only family can open (protected).

Figure — Classes — member functions, access specifiers (public, private, protected)

Why the topic needs three levels and not two: private = "nobody but me," public = "everybody." But inheritance (Inheritance in C++) creates a family case — a child class needs the parent's internal machinery while the outside world still must not see it. That family-only zone is exactly protected. This idea is the heart of Encapsulation & Data Hiding.


4. Member function — a machine bolted to the box

Why the topic needs it: the private data is unreachable from outside — the only legal doors into it are the class's own public member functions. They are the buttons of the vending machine.


5. :: — the scope resolution operator

When you write a member function's body outside the class, you must re-attach it to its class.

Why the topic needs it: void deposit(double amount) alone is a free-floating function. void BankAccount::deposit(double amount) says "this is the class's own machine," so inside it you may touch private members like balance.

void BankAccount::deposit(double amount) {  // :: ties it to the class
    balance += amount;   // legal: we're inside the wall
}

6. this — the hidden pointer to "which object"

Why the topic needs it: the same deposit code serves every object. this is how one shared machine knows whose balance to change. Deep dive in this pointer.

Figure — Classes — member functions, access specifiers (public, private, protected)

7. const after a function — a read-only promise

Why the topic needs it: it lets you call the function on const objects and documents "this only reads." Without it, const Rectangle r; r.area(); won't compile. See const correctness.


8. Putting the marks together

Read this line now, symbol by symbol:

r.setDimensions(3, 4);
  • r — an object built from a class.
  • . — reach into r.
  • setDimensions — a public member function, a button.
  • (3, 4) — inputs handed to the machine, which then guards r's private data.

Every symbol you learned above is present in that one line — that is the whole topic in miniature.


variable = named box

class = data + functions bundled

function = machine

dot operator picks a member

access specifiers three walls

member function

scope resolution ::

this pointer

const member function

TOPIC 5.2.5 Classes and Access Specifiers


Equipment checklist

Test yourself — you should be able to answer each before reading the parent note.

What does object.member mean, in plain words?
"The member belonging to this specific object" — the dot reaches into one particular box.
Class vs object in one line?
Class = the blueprint/cookie-cutter; object = one concrete thing/cookie built from it.
Do two objects share their data, their functions, or both?
Each has its OWN copy of the data; they SHARE the one copy of the member-function code.
Who can touch a public member?
Anyone, anywhere.
Who can touch a private member?
Only the class's own member functions (and friends).
What does protected add over private?
Access from derived (child) classes too.
Is access per-class or per-object?
Per-class — one object's method may read another same-class object's private data.
What does BankAccount::deposit say via the ::?
This deposit belongs to the class BankAccount, so it may touch its private members.
What does the hidden this pointer point to?
The object the member function was called on.
What does const after a member function's parameter list promise?
That the function will not modify the object; the compiler enforces it.

Connections