Hello friends, in this post today, we will learn about Access modifiers in c++ and how to use it in data hiding. so let's start....
public:-
Public is keyword.
The class members ( functions or data), which are declared as public can be accessed from outside the class.
private:-
Private members of the class can only accessed by the members functions with in that class.
Private is a keyword which is used in Data hiding and Data abstraction.
It is also used in Encapsulation.
protected:-
Protected is keyword.
This access specifier play a very important role in Inheritance.
The class members, which are declared as Protected can be accessed within the class and from derived class but cannot be accessed from any other class.
Example:-
Output:-
• In above program c is private member of class specifiers.
• So we cannot use it with object of specifiers outside the class, Means we cannot use x.c in main as x.show() or x.getdata() shown above.
NOTE:- But it can be accessed only by the member functions of it class not another class function.
Example to access private data member of class:-
Output:-
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment