Access specifiers (public, private, protected) in C++

In C++, access specifiers (Access Modifiers) are keywords used within class definitions to control the visibility and accessibility of class members (attributes and methods) from external code. There are three primary access specifiers:

  1. Public: Members declared public can be accessed from anywhere in the program.
  2. Private: Members declared private can only be accessed from within the class in which they are declared.
  3. Protected: Members declared protected can be accessed from within the class in which they are declared, as well as from derived classes.

Here's a detailed explanation with examples:

Public Access Specifier

Members declared as public are accessible from anywhere, both within the class and from external code.This specifier is often used for attributes and methods that need to be accessible and manipulated freely.

class MyClass { public: int publicVar; // Public attribute void publicMethod() { // Public method } };

Private Access Specifier

Members declared as private are only accessible within the class where they are defined. External code cannot access private members directly. This specifier is used to encapsulate and protect the internal state and implementation details of the class.

class MyClass { private: int privateVar; // Private attribute void privateMethod() { // Private method } public: void doSomething() { // This method can access private members privateVar = 42; privateMethod(); } };

Protected Access Specifier

Members declared as protected are accessible within the class and by derived classes. This specifier is typically used when creating a base class with certain members that should be accessible to its derived classes but not to external code.

class Base { protected: int protectedVar; // Protected attribute public: void baseMethod() { // Public method in the base class } }; class Derived : public Base { public: void derivedMethod() { // Accessing protectedVar from the base class protectedVar = 99; } };
Full Source Example
class Car { public: int speed; string color; private: int mileage; protected: void accelerate() { speed++; } void brake() { speed--; } }; class SportsCar : public Car { public: void turboBoost() { accelerate(); accelerate(); } }; int main() { Car myCar; // The speed and color members are public, so they can be accessed from anywhere. myCar.speed = 100; myCar.color = "red"; // The mileage member is private, so it cannot be accessed directly from outside the class. // However, we can access it indirectly using the accelerate() and brake() member functions. myCar.accelerate(); myCar.brake(); // The accelerate() and brake() member functions are protected, so they can only be accessed from within the Car class or from derived classes. // The SportsCar class inherits from the Car class, so it can access the accelerate() and brake() member functions. SportsCar mySportsCar; mySportsCar.turboBoost(); return 0; }

Advantages of using access specifiers

  1. Data hiding: Access specifiers can be used to hide data from other parts of the program. This makes the code more secure and less error-prone.
  2. Encapsulation: Access specifiers can be used to encapsulate data and behavior, which makes the code more modular and easier to maintain.
  3. Inheritance: Access specifiers are used to control how inherited class members can be accessed. This allows you to create a hierarchy of classes with different levels of access to data and behavior.

Conclusion

Access specifiers in C++ (public, private, and protected) control the visibility and accessibility of class members. Public members are accessible from anywhere, private members are only accessible within the class, and protected members are accessible within the class and by derived classes. These specifiers enable encapsulation and help manage the security and organization of class data and functionality.