1.

What are access specifiers in C++?

Answer»

Access specifiers specify the accessibility of the members of a class (attributes and methods). That is, it imposes some limitations on class members, preventing them from being DIRECTLY accessed by external functions. In C++, there are three types of access modifiers:

  • Public: Everyone will have access to all of the class members declared under the public specifier. Other classes and functions can access the data members and member functions specified as public. The direct member access operator (.) with the object of a class can be used to access the public members of that class from anywhere in the program.
  • PRIVATE: Only the member functions within the class can access the class members that have been declared as private. They can't be accessed directly from outside the class by any object or function. The private data members of a class can only be accessed by member functions or friend functions.
  • Protected: Protected access MODIFIER is similar to a private access modifier in that it can only be accessed outside of its class with the help of a friend class; but, class members defined as Protected can also be accessed by any subclass(derived class) of that class. Depending on the MODES of inheritance, this access through inheritance can CHANGE the access modifier of the members of the base class in the derived class.


Discussion

No Comment Found