Access modifiers in C++ are used to protect the members of a class. Explain each access specifier and give one example of each access specifier in C++.
Q.) Access modifiers in C++ are used to protect the members of a class. Explain each access specifier and give one example of each access specifier in C++.
Subject: Object Oriented ProgrammingAccess Specifiers in C++
Access specifiers in C++ are keywords that control the accessibility of class members (data and functions) to other classes and functions. There are three main access specifiers in C++:
Public:
- Public members are accessible from anywhere within the program.
- Example:
class MyClass { public: int public_data; void public_function() { // Function body } }; int main() { MyClass object; object.public_data = 10; // Accessing public data member object.public_function(); // Calling public member function }
Protected:
- Protected members are accessible from within the class and its derived classes.
- Example:
class MyClass { protected: int protected_data; void protected_function() { // Function body } }; class DerivedClass : public MyClass { public: void accessProtected() { protected_data = 10; // Accessing protected data member from derived class protected_function(); // Calling protected member function from derived class } }; int main() { DerivedClass object; object.accessProtected(); // Calling the function that accesses protected members }
Private:
- Private members are accessible only from within the class.
- Example:
class MyClass { private: int private_data; void private_function() { // Function body } }; int main() { MyClass object; // Error: Cannot access private members from outside the class // object.private_data = 10; // object.private_function(); }
In addition to these three main access specifiers, C++ also provides two additional access specifiers:
Friend:
- Friend functions or classes are allowed to access the private and protected members of a class.
- Example:
class MyClass { private: int private_data; public: friend void friend_function(); // Declaring a friend function }; void friend_function() { MyClass object; object.private_data = 10; // Accessing private data member from friend function } int main() { friend_function(); // Calling the friend function }
Default (Unspecified):
- If no access specifier is specified for a class member, it will have the default access specifier, which is private.
- Example:
class MyClass { int data; // Default access specifier is private void function(); // Default access specifier is private }; int main() { MyClass object; // Error: Cannot access private members from outside the class // object.data = 10; // object.function(); }
Summary Table:
Access Specifier | Accessibility | Example |
---|---|---|
Public | Accessible from anywhere within the program | public_data , public_function |
Protected | Accessible from within the class and its derived classes | protected_data , protected_function |
Private | Accessible only from within the class | private_data , private_function |
Friend | Accessible from outside the class (friend functions/classes) | friend_function |
Default (Unspecified) | Private by default | data , function |
By using access specifiers, you can control the visibility and accessibility of class members, enhancing data hiding and improving program security.