A very important feature of C++ is Data Hiding. Some of the data elements must be used within only a single class and must not be available to other classes or functions. Sometimes a data element is needed to be shared by two classes and unavailable for the rest.
To handle such situations, C++ has three Access Specifiers. These limit the scope of the variables/functions as per the requirements of the programmer.
1. public: Making a data element public means allowing the access to that data type anywhere in the program(by using “.”operator outside the class). A public data element can be edited anywhere in the program.
2. private: Making a data element private means limiting the access to that data type within the class in which it is declared and defined. the member functions of that class can use that element even outside the class but no other function or class can access that data element.
3. protected: A data element declared as protected can be accessed by the member functions of the class and those of the class which is immediately derived from it. No other class can access the protected data elements.