Access Modifiers
Access modifiers are keywords used to specify the visibility and accessibility of a class or structure’s members (attributes and methods).
They regulate how and where class members can be accessed, enabling encapsulation and safeguarding sensitive data.
These modifiers are essential in writing robust, maintainable, and secure code in Object-Oriented Programming (OOP).
Types of Access Modifiers
C++ supports three main access modifiers:
class Example {
public:
// public members
protected:
// protected members
private:
// private members
};
1. Public
Members declared as public
are accessible from anywhere in the program. They are typically used for the interface of a class, such as methods that allow interaction with private data.
2. Protected
Members declared as protected
are accessible within the defining class and derived classes. This is useful for inheritance, where child classes can access and reuse members from the parent class.
3. Private
Members declared private
can only be accessed within the defining class or structure. They hide implementation details and prevent unauthorized access or modification of sensitive data.
Example
Here’s an example of the use of access modifiers within the classes Planet
and GasGiant
:
#include <iostream>#include <string>using namespace std;class Planet {public:// ConstructorPlanet(string name, double diameter, double mass): name(name), diameter(diameter), mass(mass) {}// Public method to display planet detailsvoid displayDetails() {cout << "Planet Name: " << name << endl;cout << "Diameter (km): " << diameter << endl;cout << "Mass (kg): " << mass << endl;}protected:double diameter; // Accessible in derived classesprivate:string name; // Only accessible in this classdouble mass; // Only accessible in this class};// GasGiant inherits from the Planet class, inheriting all attributes and methods.class GasGiant : public Planet {public:GasGiant(string name, double diameter, double mass): Planet(name, diameter, mass) {}// Use of the protected attribute "diameter"void displayFeatures() {cout << "Diameter: " << diameter << " km" << endl;}};int main() {Planet earth("Earth", 12742, 5.972e24); // Create an instance of Planetearth.displayDetails(); // Call the displayDetails methodGasGiant jupiter("Jupiter", 139820, 1.898e27); // Create an instance of GasGiantjupiter.displayFeatures(); // Call the displayFeatures methodreturn 0;}
This code outputs:
Planet Name: EarthDiameter (km): 12742Mass (kg): 5.972e+24Diameter: 139820 km
Key differences between Access Modifiers
Access Modifier | Accessible in Class | Accessible in Derived Classes | Accessible in Outside Classes |
---|---|---|---|
Public | Yes | Yes | Yes |
Protected | Yes | Yes | No |
Private | Yes | No | No |
Default Accessibility in class
and struct
The keywords class
and struct
in C++ share similar functionality, with their primary distinction being the default accessibility of their members (attributes and methods).
In a struct
, the default accessibility of members is public
, whereas in a class
, it is private
. This means that unless specified otherwise, members of a struct
are accessible outside the structure, while members of a class
are not.
class MyClass {int privateData; // Private by default};struct MyStruct {int publicData; // Public by default};
Access Modifiers
- Private Access Modifiers
- Specifies that class members are only accessible from within the class itself.
- Protected
- Allows class members to be accessed within their class and by derived classes.
- Public Access Modifier
- Specifies that class members are accessible from any part of the program.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours