Ask a Teacher



is the access label private valid in member fuction declared in class,if we give defenition inside or out side the class?

 members in that section of the class. All class members are initially private by default. The labels can be in any order. These labels can be used multiple times in a class declaration for cases where it is logical to have multiple groups of these types. An access label will remain active until another access label is used to change the permissions.

We have already mentioned that a class can have member functions "inside" it; we will see more about them later. Those member functions can access and modify all the data and member function that are inside the class. Therefore, permission labels are to restrict access to member function that reside outside the class and for other classes.

Public

This label indicates any members within the 'public' section can accessed freely anywhere a declared object is in scope.

Avoid declaring public data members, since doing so would contribute to create unforeseen disasters.

Private

Members defined as private are only accessible within the class defining them, or friend classes. Usually the domain of member variables and helper functions. It's often useful to begin putting functions here and then moving them to the higher access levels as needed so to reduce complexity.

It's often overlooked that different instances of the same class may access each others' private or protected variables. A common case for this is in copy constructors.

Protected

The protected label has a special meaning to inheritance, protected members are accessible in the class that defines them and in classes that inherit from that base class, or friends of it. In the section on inheritance we will see more about it.

Other instances of the same class can access a protected field - provided the two classes are of the same type. However, an instance of a child class cannot access a protected field or method of an instance of a parent class.


comments powered by Disqus