C++ supports three access specifiers that you can use to define the visibility of classes, methods, and attributes.
public: There are no restrictions on accessing public members. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
class Test
{
public:
//Access by anyone
int data;
};
Private: Access is limited to within the class definition. This is the default access modifier type for a class if none is formally specified. They are not allowed to be accessed directly by any object or function outside the class.
class Test
{
private:
// Access only by member functions
//and friends of that class
int data;
}
Protected: Access is limited to within the class definition and any class that inherits from the class.
class Test
{
protected:
//Access by member functions and friends of that class,
//and by member functions and friends of derived classes.
int data;
};
Answer:
C++ supports three access specifiers that you can use to define the visibility of classes, methods, and attributes.
public: There are no restrictions on accessing public members. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
Private: Access is limited to within the class definition. This is the default access modifier type for a class if none is formally specified. They are not allowed to be accessed directly by any object or function outside the class.
Protected: Access is limited to within the class definition and any class that inherits from the class.