C++ Access Modifiers
C++ Access Modifiers
Public Access Modifier
All the variables and functions declared under the public access modifier will be available for everyone. They can be accessed both inside and outside the class. Dot (.) operator is used in the program to access public data members directly.
Private Access Modifier
All the variables and functions declared under a private access modifier can only be used inside the class. They are not permissible to be used by any object or function outside the class.
Protected Access Modifiers
Protected access modifiers are similar to the private access modifiers but protected access modifiers can be accessed in the derived class whereas private access modifiers cannot be accessed in the derived class.
Behavior of Access Modifiers
Derivation Type | Private Members | Protected Members | Public Members |
---|---|---|---|
Public Derivation | Not inherited | Protected | Public |
Private Derivation | Not inherited | Private | Private |
Protected Derivation | Not inherited | Protected | Protected |
Behavior of access modifiers when they are derived from public, private, and protected:
- If the class is inherited in public mode, then its private members cannot be inherited in the child class.
- If the class is inherited in public mode, then its protected members are protected and can be accessed in the child class.
- If the class is inherited in public mode, then its public members are public and can be accessed inside the child class and outside the class.
- If the class is inherited in private mode, then its private members cannot be inherited in the child class.
- If the class is inherited in private mode, then its protected members are private and cannot be accessed in the child class.
- If the class is inherited in private mode, then its public members are private and cannot be accessed in the child class.
- If the class is inherited in protected mode, then its private members cannot be inherited in the child class.
- If the class is inherited in protected mode, then its protected members are protected and can be accessed in the child class.
- If the class is inherited in protected mode, then its public members are protected and can be accessed in the child class.