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, andprotected - 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:
| Modifier | Access Scope |
|---|---|
public | Accessible from anywhere (outside or inside the class) |
private | Accessible only within the class (not from outside) |
protected | Accessible 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:
salaryis private: cannot be accessed directly (e.salary)- Only accessed via
setSalary()andgetSalary()
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:
nameisprotected: not accessible inmain(), but accessible in derived classDog
Comparison Table
| Modifier | Access Within Class | Derived Class | Outside 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, andprotectedcontrol how class members are accessed- Use
privatefor security,protectedfor inheritance, andpublicfor 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 :
