1Definition
Access Specifiers (or Access Modifiers) are keywords in object-oriented languages that define the accessibility or scope of class members (data members and member functions) from outside the class. Data Hiding is the security technique of making class attributes private so they cannot be directly viewed or manipulated from external scopes.
2Simple Explanation
Access specifiers act like Security Badges in an office building:
private: Executive office—only accessible by employees working in that specific office.protected: Family lounge—accessible by current office employees and their child branches.public: Main reception lobby—open to any member of the general public.
3Real-Life Example / Analogy
In a Bank Account:
private: Your account balance and PIN (hidden from public view).protected: Internal credit score rules (accessible by parent bank and child branch entities).public: Bank customer service hotline and branch address (accessible to everyone).
4Diagram / Visual Representation
5Working / How It Works
- During compilation, the compiler enforces access check rules.
- If code outside the class tries to access a
privateorprotectedmember directly, the compiler flags a syntax error (error: ‘int Account::balance’ is private). - Access to private data is funneled through public Getter and Setter member functions, enabling input validation.
6Types / Components / Characteristics
- Access Level Comparison:
| Access Specifier | Inside Same Class | Derived (Child) Class | Outside Class / Main |
|---|---|---|---|
private |
Yes | No | No |
protected |
Yes | Yes | No |
public |
Yes | Yes | Yes |
Note: Language extensions (like C# or Java) include internal (same assembly/package) and protected internal.
7Solved Examples / Problems
Problem Statement: Demonstrate how private access specifiers implement data hiding and how getter/setter methods protect member variables.
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Hidden data
public:
void setBalance(double b) {
if (b >= 0) { // Validation
balance = b;
} else {
cout << "Invalid Balance!" << endl;
}
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount acc;
acc.setBalance(5000.50);
cout << "Account Balance: $" << acc.getBalance() << endl;
// acc.balance = -100; // Compile Error!
return 0;
}Output:
Account Balance: $5000.58Advantages, Disadvantages & Applications
- Advantages:
- Protects internal data integrity against unintended corruption.
- Allows internal class implementation to change without breaking external client code.
- Disadvantages:
- Slight overhead due to indirect method calls for simple variable reads.
- Applications: Secure financial transaction processing, password hashing storage modules.
9Important Points to Remember
- In C++, the default access specifier for a
classisprivate, whereas for astructit ispublic. - Data hiding is not data encryption; it is an architecture-level scope constraint enforced at compile time.
10Test Your Understanding
- Conceptual Question: What is the key structural difference between a
classand astructin C++ regarding access specifiers?Show answer
Class members default toprivateaccess, whereas struct members default topublicaccess. - MCQ: Which access specifier allows member access to derived child classes but blocks access from the
main()function?- A)
public - B)
private - C)
protected - D)
internal
Show answer
C)protected - A)
- Application Question: Fix the error in
class X { int a; }; int main() { X obj; obj.a = 10; }.Show answer
Declarepublic:beforeint a;or add a public setter method, asint a;is private by default.
