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:41 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
An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase. For example the anagram of tea is tea, tae, eat, eta,

Each function has its own stack frame between %fp and %sp. Let Caller calls Callee. Then Caller's %sp becomes callee's %fp, and callee's %sp set to be a new value (a smaller one be

Write a program to illustrate array of structures? struct customer { int id; char name[15]; }; struct customer c[100]; . In the above instance, the array of Structures is

Car Rental System This system tracks cars in a rental company. Each car has a number (assume its plate number), type (small car, four wheel car), and status (rented, available). T

E v o l u t i o n of Software: In the past stage of programming the software's were developed in Machine language Using binary numbers 1's and 0's.  Later it convert

Project Description: Not just like that though. I want a similar program that can scan through poker lobbys (pokerstars, full tilt, ongame and ipoker) and seat me to the left of

C++ improves on many of C's features and provides object-oriented programming capabilities used for software production, quality and reusability. C++ was developed by Bjarne Strons

C Program for SORTING OF NUMBERS   main() {           int a[20],i,j,temp,n;           clrscr();           printf("ENTER THE MAXIMUM LIMIT: ");           scan

question c

TO CONVERT A NUMBER OR A DIGIT INTO ALPHABET