In C++, access modifiers are keywords that determine the visibility and accessibility of class members (data members and member functions). These modifiers help in enforcing data encapsulation, one of the fundamental principles of Object-Oriented Programming (OOP). Access modifiers define how and where the members of a class can be accessed, ensuring proper control over an object’s state and behavior.
The three primary access modifiers in C++ are:
These modifiers are used to specify the accessibility level of the members (variables or functions) of a class. Additionally, C++ provides a concept called default access for classes and structs.
The public access modifier allows members of a class to be accessed from anywhere: inside the class, from outside the class, and from derived classes. Public members are the most accessible, as they have no access restrictions.
class ClassName {
public:
// Public members can be accessed from anywhere
int publicVar;
void publicMethod() {
// some code
}
};
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1;
car1.brand = "Toyota"; // Public members can be accessed directly
car1.year = 2020;
car1.displayDetails(); // Calling public method
return 0;
}
brand and year, along with the method displayDetails(), are public and can be accessed directly from outside the class (i.e., in the main() function).The private access modifier restricts the access of class members to only within the class itself. Private members cannot be accessed directly from outside the class. To access or modify these members, you must use public getter and setter methods (also known as accessors and mutators).
class ClassName {
private:
// Private members can only be accessed within the class
int privateVar;
void privateMethod() {
// some code
}
};
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
void setDetails(string b, int y) {
brand = b; // Private members can be accessed inside the class
year = y;
}
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1;
car1.setDetails("Honda", 2022); // Accessing private members via public method
car1.displayDetails();
// car1.brand = "Toyota"; // Error: Cannot access private member 'brand'
// car1.year = 2023; // Error: Cannot access private member 'year'
return 0;
}
brand and year are private. They cannot be accessed directly from the main() function.setDetails() allows modifying the private members.car1.brand or car1.year directly outside the class would result in a compilation error.The protected access modifier is similar to private, with one key difference: protected members can be accessed by derived classes (subclasses), but not by outside code (not even from the main() function). This is useful in inheritance, where you want to restrict direct access but allow derived classes to access or modify certain members.
class ClassName {
protected:
// Protected members can be accessed within the class and derived classes
int protectedVar;
void protectedMethod() {
// some code
}
};
#include <iostream>
using namespace std;
class Car {
protected:
string brand;
int year;
public:
Car(string b, int y) : brand(b), year(y) {}
};
class SportsCar : public Car {
public:
SportsCar(string b, int y) : Car(b, y) {} // Accessing protected members from base class
void displayDetails() {
cout << "Brand: " << brand << ", Year: " << year << endl; // Accessing protected members
}
};
int main() {
SportsCar car1("Ferrari", 2021);
car1.displayDetails();
// car1.brand = "Porsche"; // Error: Cannot access protected member 'brand'
// car1.year = 2022; // Error: Cannot access protected member 'year'
return 0;
}
SportsCar class, which inherits from Car, can access the protected members (brand and year) of the Car class.main() function because they are not public.SportsCar has access to the protected members of the base class Car, but outside access is restricted.Class: By default, members of a class are private if no access modifier is specified.
class MyClass {
// Members are private by default
int privateVar; // private
};
Struct: By default, members of a struct are public. This is the key difference between a struct and a class in C++.
struct MyStruct {
// Members are public by default
int publicVar; // public
};
| Access Modifier | Accessibility | When to Use |
|---|---|---|
| Public | Accessible from anywhere (inside and outside the class) | Use when you want a member to be freely accessible from anywhere. |
| Private | Accessible only within the class itself | Use to protect the internal state of the object from external manipulation. |
| Protected | Accessible within the class and derived classes | Use when you want to allow derived classes to access the base class's members but restrict external access. |
#include <iostream>
using namespace std;
class Person {
private:
string name; // Private: Cannot be accessed directly from outside
int age; // Private: Cannot be accessed directly from outside
protected:
string address; // Protected: Can be accessed by derived classes
public:
Person(string n, int a, string addr) : name(n), age(a), address(addr) {}
void displayDetails() {
cout << "Name: " << name << ", Age: " << age << ", Address: " << address << endl;
}
void setName(string n) {
name = n; // Private member can be accessed within the class
}
void setAge(int a) {
age = a; // Private member can be accessed within the class
}
};
class Employee : public Person {
public:
Employee(string n, int a, string addr) : Person(n, a, addr) {}
void displayAddress() {
cout << "Address: " << address << endl; // Accessing protected member from derived class
}
};
int main() {
Person p("John", 30, "123 Main St");
p.displayDetails();
// p.name = "Jane"; // Error: Cannot access private member 'name'
// p.age = 25; // Error: Cannot access private member 'age'
Employee e("Alice", 28, "456 Oak St");
e.displayDetails();
e.displayAddress();
// e.address = "789 Pine St"; // Error: Cannot access protected member 'address'
return 0;
}
Access modifiers are essential for encapsulating data and ensuring that objects maintain integrity and adhere to the principles of information hiding in OOP. They help manage how the data is exposed or hidden from other parts of the program.
Open this section to load past papers