0
0
CppConceptBeginner · 3 min read

Access Specifiers in C++: What They Are and How to Use Them

In C++, access specifiers control the visibility of class members (variables and functions) to other parts of the program. The main specifiers are public, private, and protected, which determine whether members can be accessed from outside the class, only inside the class, or in derived classes.
⚙️

How It Works

Think of a class in C++ like a house with rooms. Access specifiers are like doors that decide who can enter which room. Public members are like rooms with open doors anyone can enter. Private members are like locked rooms only the owner (the class itself) can access. Protected members are like rooms that family members (derived classes) can enter but outsiders cannot.

This system helps keep parts of your code safe and organized. By controlling access, you prevent accidental changes or misuse of important data inside your class. It also helps you design clear interfaces for how other code can interact with your class.

💻

Example

This example shows a class with all three access specifiers. It demonstrates which members can be accessed from outside the class and which cannot.

cpp
#include <iostream>
using namespace std;

class Box {
public:
    int length;  // accessible anywhere

private:
    int width;   // accessible only inside Box

protected:
    int height;  // accessible in Box and derived classes

public:
    void setWidth(int w) {
        width = w;  // allowed because inside class
    }
    int getWidth() {
        return width;
    }
};

int main() {
    Box box;
    box.length = 10;           // OK: public
    // box.width = 5;          // Error: private
    box.setWidth(5);           // OK: public method sets private member
    cout << "Length: " << box.length << "\n";
    cout << "Width: " << box.getWidth() << "\n";
    return 0;
}
Output
Length: 10 Width: 5
🎯

When to Use

Use private for data and functions that should be hidden to protect the internal workings of your class. This prevents other parts of your program from changing important details directly.

Use public for members that form the interface of your class — the parts other code needs to use or interact with.

Use protected when you want derived classes (child classes) to access some members but keep them hidden from the rest of the program. This is useful in inheritance to share functionality safely.

In real life, this is like keeping your personal diary private, sharing your phone number publicly, and letting close friends read your notes.

Key Points

  • Public members are accessible from anywhere.
  • Private members are accessible only inside the class.
  • Protected members are accessible inside the class and its derived classes.
  • Access specifiers help protect data and define clear interfaces.
  • Choosing the right specifier improves code safety and design.

Key Takeaways

Access specifiers control who can see and use class members in C++.
Use public for interface parts, private to hide details, and protected for inheritance access.
They help keep your code safe and organized by controlling access.
Choosing the right access specifier improves code clarity and prevents errors.