access specifiers

From cppreference.com
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

In a class or struct body define the visibility of following declarators In a inheritance list, define the maximum visibility of inherited members

[edit] Syntax

public: declarators (1)
protected: declarators (2)
private: declarators (3)
class identifier : public class_name (4)
class identifier : protected class_name (5)
class identifier : private class_name (6)

[edit] Explanation

  1. The symbols declared after the specifier have public accessibility
  2. The symbols declared after the specifier have protected accessibility
  3. The symbols declared after the specifier have private accessibility
  4. The inherited members have the same accessibility as the base class ( either protected or public as private won't be visible in the derived class )
  5. The inherited members have protected accessibility in the derived class
  6. The inherited members have private accessibility in the derived class

[edit] Member accessibility by specifier

public
public members are accessible everywhere, within and outside the class scope
protected
protected members are accessible within the class and its methods and in its descendants
private
private members can be only accessed within the class and its methods

To grant access to external functions or classes to protected or private members, a friendship declaration must be present in the class body

Inherited private members are still present in the class data but cannot be accessed directly

A class has default private accessibility for inheritance and members, a struct has instead a default public accessibility