More About Access Specifiers

Submitted by: 
Language: 

In this part you will learn:
1. Types of access specifier
2. Different uses of different access specifiers
3. Using access specifiers in Code
Types of Access Specifiers
As we have discussed earlier, there are three types of access specifier we use in c++ which are given below.
• Private
• Public
• Protected

Private

This is the access specifier which is set as default. i.e when the programmer doesn't give the access specifier. C++ considers it as a private. In private access specification all the member variables and member functions written under it are considered as private members of that class. It means that those variables and functions cannot be used by any other function or in main directly. It can only be used by the functions of that class. Then the question arises, what is the purpose of using them if they are not accessible. As I stated that they can used by other member functions which means those member functions can be private too. That means we can have indirect access to those members through other private member functions. Usually to access private member variables we use getters and setters, they will be discussed in detail in the next section of this book.

Public

In public access specification, all the member functions and variables under it are use-able by any function containing that class. i.e these member can be used anywhere in the class as well as wherever its object is declared. That means the values of the member variables given under it can be changed anywhere in the program. This is the reason most programmers avoid using this access specifier with member variables. Rather they use private access specifier to have indirect access to the member variables.
Protected
This access specifier gives intermediate level of a access given by public and private. i.e It is not as restricted as private and it is as not public as Public access specifier. The actually use of it is in inheritance which we will study later.
Program

Basic Step:

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

These two are the most common and basic lines of a c++ program. The first one iostream is used to grant us most of the basic functions of C++ like Input and Output functions. The second one conio.h means console input output, the black screen we see is the console. Using namespace std; provides us an ease of using all the library functions without specifying that this function belongs to standard library. Without including it, we have to write ‘std’ with all the built-in functions .

  1. class class_name
  2. {
  3. public:
  4. int var1;
  5. int func1(){
  6. return var1;
  7. }
  8. private:
  9. int var2;
  10. int func2(){
  11. return var2;
  12. }
  13. protected:
  14. int var3;
  15. int func3(){
  16. return var3;
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. class_name temp1; //initializing a variable temp1 of type class_name
  23.  
  24. temp1.var1 = 5; //allowed because it is public member variable
  25. //temp1.var2 = 10; //not allowed because it is private member variable
  26. //temp1.var3 = 10; //not allowed because it is protected member variable
  27.  
  28. cout << temp1.func1(); //allowed because it is public member function
  29. //cout << temp1.func2(); //not allowed as it is a private member function
  30. //cout << temp1.func3(); //not allowed as it is a protected member function
  31. cout << endl;
  32. system("pause");
  33. }

The assignment of member variable var1 will only work in the above code and while printing it on the screen func1 function will only be allowed rest of them are not allowed and the reason is given as comments.

Output
picture viewer


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.