Sobes.tech
Junior — Middle

What are the access levels to class members in object-oriented programming?

sobes.tech AI

Answer from AI

In object-oriented programming, particularly in C++, there are three main levels of access to class members:

  • public — members are accessible from anywhere in the program where the object of the class is visible.
  • protected — members are accessible within the class itself and in derived classes.
  • private — members are accessible only within the class itself, not outside or in derived classes.

Example:

class Example {
public:
    int publicVar;      // accessible everywhere
protected:
    int protectedVar;   // accessible in the class and in derived classes
private:
    int privateVar;     // accessible only within the class
};