Do friends break encapsulation?, C/C++ Programming

Assignment Help:

A: No. If they're utilized properly, they increase encapsulation.

You frequently require splitting a class in half while the two halves will have distinct numbers of instances or distinct lifetimes. In these cases, usually the two halves need direct access to each other (the two halves utilized to be in the similar class, thus you haven't enhanced the amount of code that required direct access to data structure; you've simply reshuffled the code in two classes rather than one). The safest method to implement it is to make the two halves friends of each other.

If you employ friends like just defined, you'll keep private things private. People who don't understand this frequently make naive efforts to ignore using friendship in situations such as the above, and frequently they in fact destroy encapsulation. They either employ public data (grotesque!), or they make the data accessible among the halves using public get() and set() member functions. Having a public get() & set() member function for private datum is OK only while the private datum "makes sense" from outside the class (from a user's perspective). In several cases, these get()/set() member functions are approximately as bad as public data: they hide (only) the name of the private datum, however they don't hide the existence of the private datum.

Likewise, if you employ friend functions as a syntactic variant of a class's public access functions, they don't break encapsulation any more than a member function breaks encapsulation. In other terms, a class's friends don't violate the encapsulation barrier: along the class's member functions, they are the encapsulation barrier.

 (Several people think of a friend function as something outside the class. Rather then, try thinking of a friend function as part of the class's public interface. In the class declaration a friend function doesn't violate encapsulation any more than a public member function break encapsulation: both have exactly the similar authority with respect to accessing the class's non-public parts.)

 


Related Discussions:- Do friends break encapsulation?

Flow chart, flow chart for prime or not

flow chart for prime or not

Explain why a linked list is called dynamic data structure, Question: (...

Question: (a) (i) What is recursion? (ii) What advantage is there in its use? (iii) What disadvantage is there in its use? (b) The factorial of a positive number can b

What is some instance of operator overloading?, A: Here are a few of the ma...

A: Here are a few of the many instance of operator overloading: myString + yourString may concatenate two std::string objects myDate++ may increment a Date object a * b may m

User-defined data type, Explain  U s e r - D e f i n e d D a t a...

Explain  U s e r - D e f i n e d D a t a T y p es? C has Union and Structure as user-defined data types.  C++ has class which looks like structure with a

Padovan string, a padovan string p(n) for a natural number n is defined a...

a padovan string p(n) for a natural number n is defined as p(0)=''x'' p(1)=''y'' p(2)=''z'' p(n)=p(n-2)+p(n-3),n>2

Pointers, write short notes about void pointers

write short notes about void pointers

Write function that take array as argument, Write a function that takes an ...

Write a function that takes an array as the argument and returns the second largest element. Bonus (+5): Write a function that takes an array and a number n as arguments and return

Decoding , http://www.expertsmind.com/questions/decodethecode-30110560.aspx...

http://www.expertsmind.com/questions/decodethecode-30110560.aspx

Define passing structure to a function, Define Passing Structure to a Funct...

Define Passing Structure to a Function? A structure is able to be passed as a function argument identical to any other variable. If we are merely interested in one member of a

Create a stack using arrays, Problem : (a) Stacks can be implemented u...

Problem : (a) Stacks can be implemented using arrays. Write down the following C or C++ function to (i) create a stack (ii) add an element to a stack (push). (b) Expl

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd