This document will explain the three main C++ access modifiers (public, private, protected) and
illustrate their usage and impact on member accessibility with code examples. I'll generate both
DOCX and PDF versions, maintaining anonymity.
Access Modifiers in C++
What are Access Modifiers?
Access modifiers (or access specifiers) in C++ are keywords that set the accessibility of
classes, members (data and functions), and constructors. They control which parts of a program
can access the members of a class. This mechanism is a core component of encapsulation, a
fundamental OOP principle that helps in data hiding and achieving data security.
There are three main access modifiers in C++:
1. public
2. private
3. protected
By default, members of a class are private, while members of a struct are public.
1. public Access Modifier
● Members declared as public are accessible from anywhere within the program, both
inside and outside the class.
● They form the interface of the class, allowing other parts of the code to interact with the
object.
When to use public:
● For member functions that provide services or operations to the outside world (e.g.,
displayInfo(), calculateArea()).
● For data members that are intended to be directly accessible (though generally, it's better
to use public member functions to access private data).
2. private Access Modifier
● Members declared as private are accessible only from within the same class.
● They cannot be accessed directly from outside the class or from derived classes.
● This is the default access level for members in a class.
● private members are typically used for internal data storage or helper functions that
should not be exposed to the outside world, enforcing data hiding.
When to use private:
● For data members that represent the internal state of an object (e.g., salary, password).
● For helper member functions that are only used internally by other member functions of
the same class.
3. protected Access Modifier
● Members declared as protected are accessible from within the same class and from
derived classes (child classes).
● They are not accessible directly from outside the class (like private members).
● protected is primarily used in the context of inheritance (which will be covered in
Document 8). It allows derived classes to access certain members of their base class
, while keeping them hidden from the rest of the program.
When to use protected:
● For data members or member functions that are intended to be shared with descendant
classes but not exposed to arbitrary external code.
Example Program: Illustrating Access Modifiers
This program defines a Employee class with public, private, and protected members to
demonstrate their accessibility.
#include <iostream>
#include <string>
class Employee {
public: // Public members: accessible from anywhere
std::string name; // Public data member
int employeeId; // Public data member
// Public member function to set name (demonstrates accessing
private data indirectly)
void setName(const std::string& empName) {
name = empName;
std::cout << "Name set to: " << name << std::endl;
// Can access private and protected members from within the
class
calculateBonus(500); // Call private helper function
printProtectedInfo(); // Call protected helper function
}
// Public member function to display employee info
void displayPublicInfo() {
std::cout << "Employee ID: " << employeeId << ", Name: " <<
name << std::endl;
}
protected: // Protected members: accessible within this class and
derived classes
double salary; // Protected data member
// Protected member function
void printProtectedInfo() {
std::cout << " (Protected Info: Salary is accessible within
class and derived classes)" << std::endl;
// Can access private members from within protected functions
if (!privateSecret.empty()) {
std::cout << " (Also accessing private secret from
protected method)" << std::endl;
}
}
private: // Private members: accessible ONLY within this class
std::string privateSecret; // Private data member