C++ access modifiers are used for data hiding implementation. Data hiding is one of the important features of object-oriented programming, which allows the functions of a program to access directly the internal representation of a class type.The access restriction to the class members is specified by the labeled public, private, and protected sections within the class body. The keywords public, private, and protected are called access specifiers.
A class can have multiple public, protected, or private labeled sections. Each section remains in effect until either another section label or the closing right brace of the class body is seen. The default access for members and classes is private.
classBase{public:// public members go hereprotected:// protected members go hereprivate:// private members go here};
Public Access Modifier
The public access modifier defines public data members and member functions that are accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member function.
Example
The following example demonstrates the use of public access modifier −
#include <iostream>usingnamespace std;classLine{public:double length;voidsetLength(double len );doublegetLength(void);};// Member functions definitionsdoubleLine::getLength(void){return length ;}voidLine::setLength(double len){
length = len;}// Main function for the programintmain(){
Line line;// set line length
line.setLength(6.0);
cout <<"Length of line : "<< line.getLength()<<endl;// set line length without member function
line.length =10.0;// OK: because length is public
cout <<"Length of line : "<< line.length <<endl;return0;}
When the above code is compiled and executed, it produces the following result −
Length of line : 6 Length of line : 10
Private Access Modifier
The private access modifier defines private data members and member functions that cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.
By default all the members of a class would be private, for example in the following class width is a private member, which means until you label a member, it will be assumed a private member.
Example
The following example demonstrates the use of private access modifier −
classBox{double width;public:double length;voidsetWidth(double wid );doublegetWidth(void);};
Practically, we define data in private section and related functions in public section so that they can be called from outside of the class as shown in the following program.
#include <iostream>usingnamespace std;classBox{public:double length;voidsetWidth(double wid );doublegetWidth(void);private:double width;};// Member functions definitionsdoubleBox::getWidth(void){return width ;}voidBox::setWidth(double wid ){
width = wid;}// Main function for the programintmain(){
Box box;// set box length without member function
box.length =10.0;// OK: because length is public
cout <<"Length of box : "<< box.length <<endl;// set box width without member function// box.width = 10.0; // Error: because width is private
box.setWidth(10.0);// Use member function to set it.
cout <<"Width of box : "<< box.getWidth()<<endl;return0;}
When the above code is compiled and executed, it produces the following result −
Length of box : 10 Width of box : 10
Protected Access Modifier
The protected access modifier defines protected data members and member functions that are very similar to a private member, but it provides one additional benefit that they can be accessed in child classes, which are called derived classes.
You will learn derived classes and inheritance in next chapter. For now you can check following example where I have derived one child class SmallBox from a parent class Box.
Example
Following example is similar to above example and here width member will be accessible by any member function of its derived class SmallBox.
#include <iostream>usingnamespace std;classBox{protected:double width;};classSmallBox:Box{// SmallBox is the derived class.public:voidsetSmallWidth(double wid );doublegetSmallWidth(void);};// Member functions of child classdoubleSmallBox::getSmallWidth(void){return width ;}voidSmallBox::setSmallWidth(double wid ){
width = wid;}// Main function for the programintmain(){
SmallBox box;// set box width using member function
box.setSmallWidth(5.0);
cout <<"Width of box : "<< box.getSmallWidth()<< endl;return0;}
When the above code is compiled and executed, it produces the following result −
Width of box : 5
Leave a Reply