C++ Access Modifiers

Public Access Modifier in C++

Everyone will have access to all variables and functions declared with the public access qualifier. Both within the class and outside of it, they are accessible. The application uses the dot (.) operator to give users immediate access to public data members.

Private Access Modifier in C++

The class is the sole place where all variables and functions declared with a private access modifier can be used. Any object or function outside the class is not allowed to use them.

Protected Access Modifiers in C++

While protected access modifiers and private access modifiers are similar, protected access modifiers can be accessed in derived classes while private access modifiers cannot.

The behaviour of access modifiers when they are derived from public, private, and protected is displayed in the table below.

 Public derivationPrivate DerivationProtected Derivation
Private membersNot inheritedNot inheritedNot inherited
Protected MembersProtectedPrivateProtected
Public MembersPublicPrivateProtected
  • Private members of a class that is inherited in public mode cannot be passed down to a child class.
  • The protected members of a class that is inherited in public mode are accessible to child classes and are protected.
  • If a class inherits in public mode, all of its public members are accessible from both the parent class and other classes.
  • Private members of a class cannot be inherited by a child class if the class is inherited in private mode.
  • The protected members of a class that is inherited in private mode are private and inaccessible to child classes.
  • The public members of a class that is inherited privately are private and inaccessible to the child class.
  • Private members of a class that is inherited in protected mode cannot be passed down to a child class.
  • If a class inherits in protected mode, the protected members of that class are also protected and accessible from the child class.
  • If the class is inherited in protected mode, the child class can access the protected public members of the parent class.
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in