Home >>C++ Tutorial >C++ Class Access Modifiers
In this tutorial, we will be discussing about the C++ class access modifiers. Let's start with the concept behind the class access modifiers. Data hiding is one of the most important features of the Object Oriented Programming that basically permits preventing the functions of a program in order to access directly the internal representation of a class type. The access restriction to the class members is generally specified by the labeled public, private, and protected sections that are present within the class body. The keywords public, private, and protected are generally known as the access specifiers.
A class in C++ can possess multiple public, protected, or private labeled sections. Each of these sections is known to remain in effect until and unless from any one these, either another section label or the closing right brace of the class body are visible. The private is known as the default access for members and classes.
There are three types of access modifiers
Here is an example that will clear your concept regarding the access modifiers in the C++ programming language:
class Base { public: //Define public members here protected: //Define protected members here private: //Define private members here };
A public member in the C++ language is generally known to be accessible from anywhere outside of the class but not within a program. The programmers can set and obtain the value of public variables without any of the member function.
Here is an example that is depicting the same fact that has been described above. This example will help you understand the physical aspect of it:
#include <iostream> using namespace std; class Demo { public: double len; void setLen( double len ); double getLen( void ); }; //Define Member functions definitions here double Demo::getLen(void) { return len ; } void Demo::setLen( double len) { len = len; } // Main function for the program int main() { Demo demo; demo.setLen(5.0); cout << "Length = " << demo.getLen() <<endl; // set length without member function demo.len = 10.0; //Its ok becuase length is public cout << "Length = " << demo.len <<endl; return 0; }
A private member variable or a function in the C++ language cannot be accessed or even viewed from outside of the class. Private members can only be accessed by the class and friend functions. By default all of the members of a class in the C++ programming language would be private, for instance, in the following example there is a class width that is a private member that simply means that until the user label a member that member will be assumed as a private member.
Look at the example that is given below to understand the concept of the private members in the C++ language:
class Box { double width; public: double length; void setWidth( double wid ); double getWidth( void ); };
A protected member variable or the function in the C++ programming language is known to be very similar to a private member but it is known to deliver only one additional benefit that they can be accessed in the child classes that are basically called as derived classes.
Here is an example that is extremely similar to the example mentioned in this tutorial and in this example width member will be made accessible by any of the member function of its derived class SmallBox. Please observe the example carefully to understand the core concept and the physical aspect of it:
#includeusing namespace std; class Box1 { protected: double width; }; class SmallBox1:Box1 { //Here SmallBox is derived class. public: void setSmallWidth( double wid ); double getSmallWidth( void ); }; double SmallBox1::getSmallWidth(void) { return width ; } void SmallBox1::setSmallWidth( double wid ) { width = wid; } // This is the Main function of the program int main() { SmallBox1 box1; //Here need to set box width using member function box1.setSmallWidth(10.0); cout << "Width of the Box = "<< box1.getSmallWidth() << endl; return 0; }