answersLogoWhite

0


Best Answer

a class member declared as private can only be accessed by member functions and friends of that class
a class member declared as protected can only be accessed by member functions and friends of that class,and by member functions and friends of derived classes

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

We use the protected access specifier and protected inheritance specifier whenever we design a base class and wish to expose some part of its private interface to all its derived classes (and their friends) without making that interface public.

Protected access and protected inheritance do different jobs regarding the visibility of base class members. First let's consider protected access:

struct base {

public: void publicMember();

protected: void protectedMember();

private: void privateMember();

};

  • Everything that is aware of base can access base::publicMember().
  • Derivatives of base and their friends can also access base::protectedMember().
  • base and its friends can also access base::privateMember().

Note that protected access is ideally suited to member functions rather than to member data, although most technical examples will use member data to illustrate the concept. Ideally, all member data (the class representation) should be declared private to both enable encapsulation and to reduce the maintenance burden. That is, if we change the class representation in any way, only the class and its friends would be affected by those changes, thus localising the effects. So long as the public and protected interfaces remain unchanged, all consumers of our class (including existing code we cannot access and would otherwise break) would be unaffected by the changes.

Now let's consider protected inheritance:

struct publicDerived : public base {};

struct protectedDerived : protected base {};

struct privateDerived : private base {};

  • Everything that is aware of publicDerived is also aware that publicDerived derives from base.
  • Only protectedDerived, its friends, its derivatives and their friends are aware that protectedDerived derives from base.
  • Only privateDerived and its friends are aware that privateDerived derives from base.

In addition, the protected and private inheritance specifiers also change the accessibility of non-private base class members with respect to the derived class. With protected inheritance, all public members of the base class become protected members of the derived class. With private inheritance, both the public and protected members of the base class become private members of the derived class. Note that private members of a base class can never be inherited so we cannot change their accessibility with respect to the derived class.

Inheritance specifiers can also be applied to the inherited protected and public members on a member-by-member basis. For example:

structPublicDerive : public base {

protected: void base::publicMember();

private: void base::protectedMember();

};

Here, everything that is aware of publicDerived is also aware that publicDerived derives from base, as before. However, publicDerived::base::publicMember() is now a protected member of publicDerived rather than a public member, while protectedDerived::base::protectedMember() is now a private member of publicDerived rather than a protected member. Note that we can only reduce or maintain the accessibility of an inherited member, never increase it. That is, we cannot make a protected member of a base class a public member of one of its derivatives.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

When the class is expected to act as a base class and the member needs to remain private to the class, but must be accessible to the derived classes.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

protected visibility specifier is used when don't want to give direct access of the members inside it to the user but we have to or might need to inherit those members in the child class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When do you use protected visibility specifier to a class member in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is the default access specifier same as protected?

A private member of a class can only be accessed by methods of that class. A protected member of a class can only be accessed by methods of that class and by methods of a derived class of that class.


What kinds of access can a class member have?

Public, protected and private access members.


Difference between private and protected specifier in c plus plus language?

In C++, the private specifier means that the item can only be accessed by methods of the class, not including methods of derived classes. Protected, on the other hand, means the item can be accessed by methods of the class, and methods of derived classes. Public, to complete the explanation, means that the item can be acessed by any method, this class, another class, or otherwise.


If a data-item is declared as a protected access specifier then it can be accessed?

A class method or attribute (data item) that is declared protected can be accessed only by methods of the same class or by methods of derived classes of the class.


What is meant by private access in c plus plus?

It isn't. Private is the default access for class members. For struct members, the default access is public. Aside from default access, a class and a struct serve the same purpose; to define a class. As such, the following class definitions are equivalent: class X { int a; }; struct Y { private: int b; }; Typically, we use a struct to define simple data types with trivial construction and use class for more complex data types, often to encapsulate an invariant or to acquire a resource, hiding the implementation details from consumers using private access.

Related questions

Is the default access specifier same as protected?

A private member of a class can only be accessed by methods of that class. A protected member of a class can only be accessed by methods of that class and by methods of a derived class of that class.


What kinds of access can a class member have?

Public, protected and private access members.


What is private specifier in c plus plus?

The private specifier states that the member can only be accessed by the containing class, and not by any derived class, nor by any other code outside of a class.


Difference between private and protected specifier in c plus plus language?

In C++, the private specifier means that the item can only be accessed by methods of the class, not including methods of derived classes. Protected, on the other hand, means the item can be accessed by methods of the class, and methods of derived classes. Public, to complete the explanation, means that the item can be acessed by any method, this class, another class, or otherwise.


If a data-item is declared as a protected access specifier then it can be accessed?

A class method or attribute (data item) that is declared protected can be accessed only by methods of the same class or by methods of derived classes of the class.


What is meant by private access in c plus plus?

It isn't. Private is the default access for class members. For struct members, the default access is public. Aside from default access, a class and a struct serve the same purpose; to define a class. As such, the following class definitions are equivalent: class X { int a; }; struct Y { private: int b; }; Typically, we use a struct to define simple data types with trivial construction and use class for more complex data types, often to encapsulate an invariant or to acquire a resource, hiding the implementation details from consumers using private access.


Explain the significance of public and protected and private access specifiers in inheritance?

These are all access modifiers in Java. a. Public - these are accessible anywhere. This is the least restrictive access specifier. b. Private - these are accessible only inside the declaring class. This is the most restrictive access specifier. c. Protected - these are in between public and private. These are accessible to all classes that inherit this class d. Package - this is the default access specifier. These are accessible to all classes that are present in the same package as the contained class.


When do you use the protected access specifier on a class member in c plus plus?

You use the protected access specifier to allow a derived class implementer access to what would otherwise be a private member method of your class. Private member methods are intended for internal use only, however some may be optimised versions of public member functions. Public member functions, particularly accessors (getters) and mutators (setters), often contain additional runtime checks that are usually redundant to the internal implementation. While we may not wish to allow "ordinary" users access to these optimised functions, derived class implementers are not ordinary users. Like you (the class designer) they want to create an efficient implementation and would therefore benefit from having access to your private implementations, or at least some of them. We achieve this by declaring those methods protected. Note that although you can also allow class implementers protected access to your otherwise private member data, this is not recommended as class implementers would then be able to undermine your data encapsulation. Class representations must remain private to maintain encapsulation. Similarly, any private methods that have potential for undermining encapsulation should likewise remain private.


What is the use of public access specifier?

use of public access specifier iswe can access the class members(methods,variables) out side the class using class reference


What is visibility mode what are the different inheritance visibility modes support by c plus plus?

There is no such thing as visibility mode in C++. Visibility is a function of information hiding but that relates to the way in which implementation details can be obfuscated within binary executables and libraries where only the interface need be exposed in a plain-text header file. This has nothing whatsoever to do with object oriented programming since information hiding is also possible in C. You probably meant access specifiers. There are three levels: private, protected and public. Private access limits access to the class and to friends of the class. Protected is the same as private but extends access to derivatives of the class. Public access imposes no limits. In terms of inheritance, the specified access level determines the accessibility of the protected and public members of the base class (private members are never inherited and will always remain private to the base class). in essence, members with access greater than the specified inheritance are reduced to the specified access. Thus if you specify protected inheritance, all public members of the base class become protected members of the derivative, while private inheritance reduces all public and protected members to private access. You may also reduce access to specific base class members simply be redeclaring them with the appropriate access.


Which access limitation is found in a class member declared protected internal?

"Internal" is not a C++ keyword, so it is meaningless in this context. "Protected" means that the class member is visible to (has scope from) only the class and classes derived from the class.


Why protected access specifier is not in interfaces?

Because an interface is like a contract/skeleton which decides what the implementing class has to do. So, if any entity in an interface is protected, they would not be available to the class that is implementing the interface. Hence, all the variables and methods declared inside an interface are public by default