C++ Class Access Specifiers

Access specifiers are an important feature in C++ that control the visibility and accessibility of the data members and member functions of a class.

In C++, there are three main access specifiers:

  1. public - members and methods are accessible from outside the class
  2. private - members and methods cannot be accessed from outside the class
  3. protected - members and methods cannot be accessed from outside the class, however, they can be accessed in inherited classes.

In this article, we will explore the basics of C++ class access specifiers and how they are used to control access to class members.

Public Access Specifier

The public access specifier is used to declare members of a class that can be accessed from anywhere in the program. Members declared as public are visible and accessible to all parts of the program, including other classes and functions. For example, consider the following code:

1class Rectangle { 2 public: 3 int width, height; 4}; 5 6int main() { 7 Rectangle rect; 8 rect.width = 10; 9 rect.height = 20; 10 return 0; 11}

In this example, the width and height members of the Rectangle class are declared as public, so they can be accessed from the main function.

Private Access Specifier

The private access specifier is used to declare members of a class that can only be accessed within the class. Members declared as private are not visible or accessible from outside the class. For example, consider the following code:

1class Rectangle { 2 private: 3 int width, height; 4 public: 5 int getWidth() { 6 return width; 7 } 8 int getHeight() { 9 return height; 10 } 11}; 12 13int main() { 14 Rectangle rect; 15 rect.width = 10; // Error: width is private 16 rect.height = 20; // Error: height is private 17 return 0; 18}

In this example, the width and height members of the Rectangle class are declared as private, so they cannot be accessed from the main function. Instead, the getWidth and getHeight member functions are provided to allow access to these members.

Protected Access Specifier

The protected access specifier is similar to private, but with a few key differences. Members declared as protected can only be accessed within the class and by its subclasses. This allows subclasses to access and inherit the protected members of a class, while still maintaining their visibility and accessibility restrictions. For example, consider the following code:

1class Shape { 2 protected: 3 int width, height; 4}; 5 6class Rectangle : public Shape { 7 public: 8 int getWidth() { 9 return width; 10 } 11 int getHeight() { 12 return height; 13 } 14}; 15 16int main() { 17 Rectangle rect; 18 rect.width = 10; // Error: width is protected 19 rect.height = 20; // Error: height is protected 20 return 0; 21}
1error:int Shape::width’ is protected within this context

In this example, the width and height members of the Shape class are declared as protected, so they cannot be accessed from the main function. However, they can be accessed by the Rectangle class because Rectangle is a subclass of Shape.