answersLogoWhite

0


Best Answer

The private, protected and public keywords are used to modify the access specifiers of class or struct members. Unless otherwise specified, class members are private by default, while struct members are public by default.

Private members of a class are only accessible to members and to friends of that class. Protected members are the same as private members, but are also accessible to derived classes. Public members have unrestricted access.

The private, protected and public access specifiers can also be used to modify the type of inheritance that applies to a derived class. Private inheritance means all public and protected members of the base class become private members of the derived class. Protected inheritance means all public members of the base class become protected members of the derived class. Public inheritance means all public and protected members of the base class remain public and protected members of the derived class. Private members of the base class are never inherited by derived classes. A derived class or one or more of its member functions may be declared a friend of the base class, thus permitting private access, but you would never do this unless the hierarchy were a closed, static hierarchy where all derivatives can be determined at compile time. Dynamically bound derivatives of unknown origin cannot be declared friends.

User Avatar

Wiki User

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

Wiki User

16y ago

We use these keywords to specify access levels for member variables, or for member functions (methods). Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.

Deciding when to use private, protected, or public variables is sometimes tricky. You need to think whether or not an external object (or program), actually needs direct access to the information. If you do want other objects to access internal data, but wish to control it, you would make it either private or protected, but provide functions which can manipulate the data in a controlled way

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Protected and friend are completely unrelated keywords. The best I can do is just tell you what they are and how they are used.

Protected is a class access specifier. Class interfaces have three levels of access: private, protected and public. Every member of a class must fall into one of these categories. If no access specifier is specified, private is implied (classes are private by default). Access specifiers apply to all the members that follow the specifier, up to the next specifier, but they can appear in any order within a class declaration, and can be repeated as often as required. How you organise the members of your class is entirely up to you, but you must be clear about which members are exposed (the public interface) and which are hidden inside the class (private or protected).

Private members are accessible to the class itself (including other instances of the class) and also to friends of the class. Protected members are the same as private members, but are also accessible to classes derived from the class. Public members are accessible from anywhere (inside the class, outside the class, friends and non-friends alike).

A friend is a function, class, or class method that is external to the class in which it is declared a friend, but has privileged access to the private and protected members of the class (as well as the public members of course). Friends can be declared anywhere in a class declaration regardless of the access specifier that is in force at that point in the declaration. Access specifiers are irrelevant to friend declarations because friends are not members of the class. Although they have the same access rights as the class itself, they do not have an implicit this pointer because, again, they are not members of the class.

Since friends are highly privileged, granting friend access to a class should be used sparingly. The only reason to declare a friend is when the friend requires access to the private or protected members of the class. If a friend can achieve its purpose via the public interface, then it has no business being a friend. However this does not mean the public interface should be engineered to suit the friend simply to avoid granting it friend access.

Many will tell you friends are bad or evil as they actively undermine the principals of OOP and should therefore be avoided at all costs. Quite frankly this is complete nonsense. If the only alternative is to make private members public, then that alone will do far more to undermine any concept of encapsulation than simply granting friend access. However, you should also consider whether it is better to implement the friend function as a member method of the class, rather than as a friend. Sometimes this is not possible, but that can often be an indication of poor class design. If not, then friend access is the only option.

Ultimately, friends cannot undermine OOP any more than the class itself can undermine its own encapsulation (it has the exact same access rights, after all). A friend is effectively encapsulated by the class; it is merely an extension of the interface and, if anything, reinforces OOP rather than undermines it. That said, prevalent use of friends are often an indication of poor class design. While it may help you meet deadlines in the short term, it may lead to grief in the long term. Always give due consideration to the alternatives. If a class must be redesigned, it's better to do it sooner rather than later, even if it means missing that all-important deadline.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Private means that the member data or function cannot be accessed by code outside of the containing class.

Protected means that the member data or function cannot be accessed by code outside of the containing class or outside of a class that inherits the class.

This answer is:
User Avatar

User Avatar

Wiki User

17y ago

"Inheritance" has nothing to do with "access modifiers". So there is no public inheritance or private inheritance

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between protected and friend in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


What is the difference between private stafford and plus student loans?

What is the difference between private stafford and plus student loans?


What is the difference between pointers in c and c plus plus?

Nothing.


What is the difference between be plus ing and get plus ing?

There are no such terms in C++.


What is the difference between statements in c plus plus charconstp and char constp?

There is no difference. Both statements are invalid.


What is the difference between Nike plus and Nike plus Active?

They have different names


What is the difference between plus 8 and -12?

-4


What is the difference between DVD plus r and DVD plus rw?

rightable and rewightable


Difference between void and devoid in c plus plus?

There is no such thing as devoid in C++.


What is the difference between minus 4 and plus five?

9.


Calculate the difference between -2.00 and plus 1.75?

3.75 is the answer.