The Wayback Machine - https://web.archive.org/web/20120308091834/http://en.cppreference.com:80/w/cpp/language/access

access specifiers

From Cppreference

Jump to: navigation, search

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

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages