Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

access-specifiers

Access specifiers

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Access specifiers: the syntax, patterns, and safety habits you need before advancing in C++.

Teams still ship Access specifiers in C++ codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Access specifiers in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Access specifiers enforce encapsulation: public, protected, and private control who can reach members—like Java but with struct defaulting to public.

Encapsulation pattern

class BankAccount {
public:
    void deposit(int cents) { balance_ += cents; }
    int balance() const { return balance_; }
private:
    int balance_ = 0;
};

Keep data private; expose small, validated public interfaces.

Important interview questions and answers

  1. Q: friend keyword?
    A: Grants specific functions/classes access to private members—use sparingly.
  2. Q: protected vs private?
    A: protected visible to derived classes; private only within the class (and friends).

Self-check

  1. Default access in struct vs class?
  2. Why keep fields private?

Tip: struct defaults to public; class defaults to private—choose class for types with invariants.

Interview prep

private members?

Accessible only within the class (and friends)—enforces encapsulation.

friend functions?

Granted access to private members—use sparingly for tightly coupled helpers.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • friend when?
  • protected use?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump