C++: Public, private and protected

This entry is part 42 of 61 in the series C++

The 3 keywords can be applied to:

  1. classes and
  2. data members and member functions.

We first look at applying them to data members or functions.

When you declare the members as public, the public keyword specifies that those members are accessible from any function, including functions in the derived class.

Take note that the default access of members in a class is private.  So if the 3 keywords are omitted, the members will be treated by the compiler to be of private members.

When private keyword is applied to members, it specifies that those members are accessible only from member functions of the same class.  They can’t be accessed by the derived members of the class.

When you declared members as protected, only member functions of the same class and its derived class could access them.



Series Navigation<< C++: Array
C++: Applying public, private and protected keywords to the base class itself >>

Leave a comment

Leave a Reply