C++ Class Access Modifiers
One of the most important features of Object Oriented Programming is Data hiding. It allows the developer to obstruct the functions of a program from directly accessing the internal representation of a class type. The restriction of access of the class members can be customised by the keywords public, private, and protected. These are called access specifiers.
A class can have multiple public, protected, or private defined sections. Each section is effective as long as it does not encounter another section which has a different access specifier, or it reaches the closing right brace of the class body. The default access for members and classes is private.
class Sample { public: // public members go here protected: // protected members go here private: // private members go here };
The public Members:
A member of the class, which is defined under the public access specifier is accessible from anywhere outside the class but within a program. We can set and get the value of public variables without any member function. For Example:
#include <iostream> using namespace std; class Road { public: float len; void setLen( float l ); float getLen( void ); }; // Member functions are defined here float Road::getLen(void) { return len ; } void Road::setLen( float l) { len = l; } // Main function int main() { Road road; // set road len road.setLen(7.0); cout << "Length of road : " << road.getLen() <<endl; // set road length without member function road.len = 8.0; cout << "Length of road : " << road.len <<endl; return 0; }
Output:
Length of line : 7
Length of line : 8
The private Members:
A member that is declared under the private keyword cannot be accessed, or even viewed from outside the class. Only the class and friend functions will be able to gain access to the private members.
By default, if no access specifier is mentioned, then all the members of a class would be private.Essentially, we define data under the private section and related functions in public section so that they can be called from outside of the class as demonstrated in the following program:
#include <iostream> using namespace std; class Room { public: float len; void setWid( float w ); float getWid( void ); private: float wid; }; // Member functions are defined here float Room::getWid(void) { return wid ; } void Room::setWid( float w ) { wid = w; } // Main function int main() { Room room; // set room length without member function room.len = 20.0; cout << "Length of room is: " << room.len <<endl; // set box width without member function // room.wid = 20.0; // Error: because width is private room.setWid(20.0); // Use member function to set it. cout << "Width of room : " << room.getWid() <<endl; return 0; }
Output:
Length of room : 20
Width of room : 20
The protected Members:
A member defined under the protected keyword is very similar to a private member, with the small exception that it provides the benefit that they can be accessed by child classes which are known as derived classes.
Login/Signup to comment