Avicii100
Blockchain Technology Evangelist
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:
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 .
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
Book traversal links for More About Access Specifiers
Download
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:
- #include<iostream>
- #include<conio.h>
- 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 .
- class class_name
- {
- public:
- int
var1;
- int
func1(
)
{
- return
var1;
- }
- private:
- int
var2;
- int
func2(
)
{
- return
var2;
- }
- protected:
- int
var3;
- int
func3(
)
{
- return
var3;
- }
- }
;
- int
main(
)
- {
- class_name temp1;
//initializing a variable temp1 of type class_name
- temp1.var1
=
5
;
//allowed because it is public member variable
- //temp1.var2 = 10; //not allowed because it is private member variable
- //temp1.var3 = 10; //not allowed because it is protected member variable
- cout <<
temp1.func1
(
)
;
//allowed because it is public member function
- //cout << temp1.func2(); //not allowed as it is a private member function
- //cout << temp1.func3(); //not allowed as it is a protected member function
- cout <<
endl;
- system
(
"pause"
)
;
- }
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

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.