Access Specifiers in C++

category: 

As we know that there are three types of access specifier that is Private, Public and protected. we have discussed "Protected" access specifier here.

Private

The visibility of the Private access specifier is limited to only for a class where it is defined. In other words, it can only be accessed by the definition of the class. The declaration of int y in private part of the class date in program dateclass.cpp is restricted to this class and can only be accessed by the member functions of the class date and not by any other part of the program, e.g function main() cannot access it directly. This is the ability of C++ programming language and is called data hiding. This protects data from misuse. The keyword private followed by the colon (: ) is used to make member data visible within the class only. By default it is also private.

Having discussed the private part of the class, let us design a class with private data members and access them outside the class.

Program Example

  1. //program name: private.cpp
  2. #include
  3. #include
  4. class minimum
  5. {
  6.  private:
  7.             int x, y;
  8.             public
  9.             void findmin()
  10. {
  11.             if(x
  12.             cout<<”x(“<
  13.             else
  14.             cout<<”y(“<
  15. }
  16. };
  17. int main(void)
  18. {
  19. class minimum m;
  20. clrscr();
  21. m.x = 10;
  22. m.y = 20;
  23. m.findmin();
  24. return(0);
  25. }

There are two compilation errors in the program private.cpp which are listed below.

‘minimum::x’ is not accessible

‘minimum::y’ is not accessible

Public

Usually the data members are not declared in this part. The member functions are usually declared in this part. The public accesses specifier gives access to the member function from outside the class. This part allows the programmer of a class to easily change the data member of the class. In other words, it gives flexibility of reading and writing the data members of the class from anywhere in the class. We can also declare data member in the public part but not in 95% cases. The keyword public followed by the colon (: ) means to indicate the data member and member function that are visible outside the class.

Having the same program private.cpp, change private to public. This time it will proceed to a successful compilation. Upon execution of the program, it will produce the correct result as shown next.

Program Example

  1. //program name: date1.cpp
  2. #include
  3. #include
  4. class minimum
  5. {
  6.             public:
  7.             int x,y;
  8.             void findmin()
  9. {
  10.             if(x
  11.             cout<<”x(“<
  12.             else
  13.             cout<<”y(“<
  14. }
  15. };
  16. int main(void)
  17. {
  18.             class minimum, m;
  19.             clrscr();
  20.             m.x = 10;
  21.             m.y = 20;
  22.             m.findmin();
  23.             return 0;
  24. }

Run Output

X(10) is smaller than Y(20)

Y(20( is smaller than X(30)

The use of specifiers

One might say, what is a complex class? A class that encompasses many data members and member functions. A class having that is a total of one hundred member data and member functions can be very complicated to write. Such class will lead to a time consuming job. It will be easy if there are four or five member functions in public access specifier and the data member in the private access specifier. In this case, there will be no confusion to understand how to use the class. It will not bother you because the private access specifier is not accessible without performing any operation on the member functions. In a short program with a page or two, the use of these specifiers may seem no value but with a large program having 20 or more pages, It is worth understanding. Remember, those functions which are called outside the class should be public and data member is generally be private. Another important point should be noted that the mentioned access specifiers plus the protected one affects all data member and member functions until the next occurrence of an access specifier.