intermediate18 min

Encapsulation and Access Specifiers

Controlling visibility of a class's members with public, private, and protected.

What you'll learn

  • Explain what public, private, and protected each control on a class member
  • Use private data with public methods to enforce encapsulation
  • Predict that outside code can only call a class's public interface, not touch its private data directly

Explanation

Encapsulation means bundling an object's data together with the methods that operate on it, and restricting direct outside access to that data -- outside code interacts with the object only through a deliberately chosen public interface, never by reaching in and touching its internals directly. C++ enforces this with three access specifiers: public (accessible from anywhere), private (accessible only from within the class's own member functions), and protected (like private, but also accessible to derived classes, which matters once you reach inheritance later in this course).

A common pattern is keeping data members private and exposing controlled access through public methods: a deposit(amount) method can validate or adjust an account's balance in a controlled way, while a getBalance() method exposes a read-only view of it, without ever letting outside code assign directly to the balance field and bypass whatever rules the class wants to enforce. Attempting to access a private member from outside the class is a compile error, not a warning -- the compiler genuinely enforces the boundary, it isn't just a naming convention like it is in some other languages.

Everything after an access specifier keyword (until the next one, or the closing brace) shares that access level -- you don't need to repeat private: before every single member, just once when the access level changes.

Guided lab

Fill in the blank: making balance_ private

C++Not executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, fill in the missing piece, then reveal the completed code and its expected output.

Fill in the missing access specifier, then predict the output.

#include <iostream>

class BankAccount {
public:
    BankAccount(double balance) : balance_(balance) {}

    void deposit(double amount) {
        balance_ += amount;
    }

    double getBalance() const {
        return balance_;
    }

____:
    double balance_;
};

int main() {
    BankAccount account(100.0);
    account.deposit(50.0);
    std::cout << "Balance: " << account.getBalance() << std::endl;
    return 0;
}

Stuck? Get a hint.

Common mistakes

  • Making all data members public 'to keep things simple,' which defeats the point of encapsulation and lets outside code bypass any validation the class wants to enforce.
  • Trying to access a private member directly from outside the class, forgetting the compiler enforces this boundary strictly, not just as a convention.
  • Repeating an access specifier before every single member instead of understanding it applies to everything until the next access specifier or the class's closing brace.

Knowledge check

Knowledge check

1. Which access specifier makes a class member accessible from outside the class?
2. What happens if outside code tries to access a private member directly?
3. What is encapsulation?

Takeaway

Keep data members private and expose a deliberate public interface -- the compiler genuinely enforces private access from outside the class, not just as a naming convention.

Summary

public/private/protected control member visibility; encapsulation keeps data private behind a controlled public interface, enforced by the compiler as a real boundary, not a convention.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.

Next: struct vs class