Q:

What are C++ access modifiers?

0

What are C++ access modifiers?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

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;
};
 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What are the differences between a class and a str... >>
<< Why use access modifiers in C++?...