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

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.)

 

Posted Date: 3/20/2013 4:03:17 AM | Location : United States







Related Discussions:- Do friends break encapsulation?, Assignment Help, Ask Question on Do friends break encapsulation?, Get Answer, Expert's Help, Do friends break encapsulation? Discussions

Write discussion on Do friends break encapsulation?
Your posts are moderated
Related Questions
Your program should use the Free Pascal compiler or the Lazarus IDE described in CSC 540. When you hand in your assignment to the digital dropbox, it should be a .PAS file, not zip

3. Write a program to encrypt and decrypt strings of characters using the following ciphers: a) Caesar cipher b) Vigenere cipher c) Matrix transposition cipher Your program shoul

program for lexicographically precedes the given input permutation

Receive 3 numbers and display them in ascending order from smallest to largest ed#

a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between tw

Write a function that has a single int parameter n and returns an int: 0 if n is negative, otherwise 1. You are not allowed to simply check the integer value, and should use the fu

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 where + denotes string concatenation. For a string of the c

Create a program WordCount1Main.java doing the following:  For each word in the le word.txt { Create an object of the class Word { Add the object to a set of the type java.uti

I need Profile and optimize a C++ library for speed Project Description: Optimize and profile an existing C++ library for speed. This library uses OpenMP and OpenCV and C+

C Program for MAXIMUM NO, MINIMUM NO AND SORTING   void main() {           int i,j,t;           int a[5];           clrscr();           for(i=0;i