• We just launched and are currently in beta. Join us as we build and grow the community.

More About Access Specifiers

Avicii100

Blockchain Technology Evangelist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
50
Likes
36
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. int

    main(

    )
  20. {
  21. class_name temp1;

    //initializing a variable temp1 of type class_name

  22. temp1.var1

    =

    5

    ;

    //allowed because it is public member variable
  23. //temp1.var2 = 10; //not allowed because it is private member variable
  24. //temp1.var3 = 10; //not allowed because it is protected member variable

  25. cout <<

    temp1.func1

    (

    )

    ;

    //allowed because it is public member function
  26. //cout << temp1.func2(); //not allowed as it is a private member function
  27. //cout << temp1.func3(); //not allowed as it is a protected member function
  28. cout <<

    endl;
  29. system

    (

    "pause"

    )

    ;
  30. }

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
2.more_about_access_specifiers.jpg


Book traversal links for More About Access Specifiers

  • ‹ Getting Started With Object Oriented Programming
  • Up
  • Setters And Getters ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

451,993

323,299

323,308

Top