🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C++ Access Modifiers

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.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.