🧱 C++ Object-Oriented Programming
Estimated reading: 3 minutes 267 views

C++ Access Modifiers – Control Class Member Visibility


Introduction – Why Access Modifiers Matter in C++

In C++ programming, access modifiers define how members of a class (variables and functions) can be accessed. This is crucial for encapsulationβ€”protecting object data and controlling how it is modified or viewed.

In this guide, you’ll learn:

  • What access modifiers are and how they work
  • The three types: public, private, and protected
  • How they support object-oriented principles
  • Real-world examples with clear outputs

What Are Access Modifiers?

Access modifiers determine the scope and visibility of class members. C++ supports three access levels:

ModifierAccess Scope
publicAccessible from anywhere (outside or inside the class)
privateAccessible only within the class (not from outside)
protectedAccessible within the class and its derived (child) classes

Code Examples – With Output & Walkthrough

Example 1: private and public Use

#include <iostream>
using namespace std;

class Employee {
private:
    int salary;

public:
    void setSalary(int s) {
        salary = s;
    }
    int getSalary() {
        return salary;
    }
};

int main() {
    Employee e;
    e.setSalary(50000);
    cout << "Salary: " << e.getSalary() << endl;
    return 0;
}

Output:

Salary: 50000

Explanation:

  • salary is private: cannot be accessed directly (e.salary )
  • Only accessed via setSalary() and getSalary()

Example 2: protected in Inheritance

#include <iostream>
using namespace std;

class Animal {
protected:
    string name;
public:
    void setName(string n) { name = n; }
};

class Dog : public Animal {
public:
    void bark() {
        cout << name << " says: Woof!" << endl;
    }
};

int main() {
    Dog d;
    d.setName("Buddy");
    d.bark();
    return 0;
}

Output:

Buddy says: Woof!

Explanation:

  • name is protected: not accessible in main(), but accessible in derived class Dog

Comparison Table

ModifierAccess Within ClassDerived ClassOutside Class
public Yes Yes Yes
protected Yes Yes No
private Yes No No

Best Practices & Tips

Best Practice: Use private for member variables and expose access via public functions.

Tip: Prefer protected over public in inheritance when internal access is needed but not public exposure.

Pitfall: Exposing variables as public can lead to insecure or unstable code.


Use Cases

Security Systems: Use private to restrict access to sensitive user data
πŸ‘©β€πŸ‘§β€πŸ‘¦ OOP Inheritance Models: Use protected to share behavior with child classes
Encapsulation & Validation: Use getters/setters to validate input before modifying private variables


Summary – Recap & Next Steps

Key Takeaways:

  • public, private, and protected control how class members are accessed
  • Use private for security, protected for inheritance, and public for interface
  • They enable encapsulation, a core pillar of OOP

Real-World Relevance:
Access modifiers are used extensively in class-based designs like APIs, device drivers, banking systems, and more.

Next Steps:

  • Learn about Static Members
  • Explore Friend Functions and how they bypass access rules

FAQ – C++ Access Modifiers

Can I access private members directly?
No, private members are not accessible outside the class. Use public member functions.

When should I use protected over private?
Use protected when you need derived classes to access the base class members.

What happens if I don’t specify an access modifier?
By default, class members are private and struct members are public.

Can I make only a few functions public?
Yes. You can control access per function or variable by placing them in the right access section.

Can friend functions access private/protected members?
Yes. A friend function or class can access all members, regardless of their access level.


Share Now :
Share

C++ Access Modifiers

Or Copy Link

CONTENTS
Scroll to Top