Namespaces
Variants
Actions

Access specifiers

From cppreference.com


Access specifiers are used in two places by C++. The first is to enable the developer to specify the visibility of members (attributes and methods) of structures (struct) and classes (class). The second is to enable the developer to control the visibility of the members of a parent class or struct from a subclass. This section will address the use of access specifiers with regard to the visibility of members. The use of access specifiers for inheritance is covered in the section on inheritance.


Access specifiers describe who is allowed to access a member (attribute or method) of a class or struct.


The following specifiers are available in C++:

public
Everybody has access to this member function or member attribute. This is the default visibility for struct members.
protected
This member is only available to this class or any class that inherits this class.
private
Only this class can access the members. This is the default visibility for class members.


The use of an access specifier creates a visibility scope with a class or struct. Once declared, the visibility of members remains the same unless another access specifier is encountered.

class Example
{
    // as default everything in a class is private
 
public:
    // everything from here on is public until a different specifier appears
 
protected:
    // everything from here on is protected
 
private:
   // everything from here on is private
};


struct example
{
    // by default, members are public in a struct
private:
    // nothing in this section is visible / accessible from anywhere except 
    // inside the struct itself
 
protected:
    // members in this section are visible to subclasses
 
public:
    // members in this section are visible to all
    // 
};