アクセスレベルを設定するキーワード。
public- どこからでもアクセスできる
- 構造体(C++)のメンバのデフォルト
- protected
- そのクラス自身と派生クラスからアクセスできる
- 外部からはアクセスできない
private- そのクラス自身からのみアクセスできる
- クラス(C++)のメンバのデフォルト
構造体(C++)を集成体として使いたい場合は、メンバをすべてpublicにし、privateやprotectedを使わない構成にすることが多い。
例
class Date
{
// Any members defined here would default to private
public: // here's our public access specifier
void print() const // public due to above public: specifier
{
// members can access other private members
std::cout << m_year << '/' << m_month << '/' << m_day;
}
private: // here's our private access specifier
int m_year { 2020 }; // private due to above private: specifier
int m_month { 14 }; // private due to above private: specifier
int m_day { 10 }; // private due to above private: specifier
};
int main()
{
Date d{};
d.print(); // okay, main() allowed to access public members
return 0;
}