Class Access Levels

April 20th, 2009 by carlos


C++ provides three access levels for members of a class. Both member variables and member functions can be tagged as public, private, or protected, depending on the intended use of the member in the class.

A public data member of member function is one that can be accessed by any user of the class, independent of where the user is located. Thus, member functions that are declared public can be accessed anywhere, and represent the public interface of a C++ class.

Public member variables are also allowed to be viewed anywhere in the program. However, allowing this to happen is usually not a good idea, since it would inibit future changes in the representation of the data stored in a class. Unless you really need to do this for some reason (for example, compatibility with C code), you should avoid the creation of public member variables in a C++ class.

Private member variables, on the other hand, are not visible outside the class where they are declared. This is the preferred way of declaring member variables in C++, since it guarantees that the usage of data is limited to the class itself.

Like private member variables, private member functions are also useful in C++ programs. Such member functions are used in the implementation of the class, and are not accessible to users of the class. Every member function that is not part of the interface should be made private (or at least protected).

In fact, C++ defines private access as the default level of access for class member. That is, if you don’t specify anything as the access level, then the member will be considered private.

The other level of accessibility is the protected level. A member function or member variable is protected if it is accessible only inside a class or by the classes that derive from it. Therefore, only classes that inherit from the class where the member is contained can have access to the function or variable.

Protected members of a class are useful to provide functionality for derived classes. However, they should be used with care. One of the main problems, is that classes that expose too many details to derived classes may have difficulties when changing their internal representation, since an unspecified number of derivate classes may be using the protected information.


Example

Here is an example of the access level described above. Note that the method Director::getInfo() incorrectly tries to access getAge, since it is a private method.

class Employee {
  // private member variable
  int age;
  // private member function
  int getAge() { return age; }
public:
  // public member variable (don't do this!)
  std::string name;
  std::string getName() { return name; }
protected:
  // these members are visible only inside Employee
  // and derived classes
  std::string telephone;
  std::string getTelephone() { return telephone; }

};

// A derived class
class Director : public Employee {
   public:
   void getInfo();
};

void Director::getInfo() {
   // this works - protected
   cout << getTelephone() << "\n";
   // this doesn't work - private to Employee
   cout << getAge() << "\n";
}

Important

Don’t forget to add the public: tag before public members. Remember that the default access level in C++ is private.