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?

Which one would you prefer - a macro or a function, Which one would you pre...

Which one would you prefer - a macro or a function? Actually it depends on the purpose of program! - In case of macros, corresponding code is inserted directly into your sou

Working with files in cpp, Operation Files 2.1. Your program should open an...

Operation Files 2.1. Your program should open and read the file specified by the first command line argument of the program. 2.2. The first line of the file contains an integer

C program to print l diagonal triangle, C program to print L diagonal trian...

C program to print L diagonal triangle: void main() {                 int i=0,j=0;                 int arr[rows][cols];                 for (i=0; i

Online poker web application, Project Description:  LOOKING TO DEVELOP ...

Project Description:  LOOKING TO DEVELOP A ONLINE POKER Web Application. WEBSITE WILL BE DEVELOPED OVER THE NEXT 6 MONTHS Duration & WILL REQUIRE FULL SOFTWARE CODING OF WEB

What are some ways try / catch / throw can develop software , Q: What are s...

Q: What are some ways try / catch / throw can develop software quality? A: By removing one of the reasons for if statements. The commonly utilized alternative to try / catch

Fraction coding, Create a program that asks the user for two integers which...

Create a program that asks the user for two integers which represent the numerator and denominator parts of fraction. Print out a simplification of the fraction. Example: Enter th

Poop, hwat is the area fsdjlakl;aeklfjtealrtl;gka

hwat is the area fsdjlakl;aeklfjtealrtl;gka

Define the float data type of c language, Define the Float Data Type of c L...

Define the Float Data Type of c Language? The float is used to define floating point numbers. The Floating point numbers are stored in 32 bits with 6 digits of precision. Eg

Describe faster ++i or i++, A: The answer to this lies actually, how they u...

A: The answer to this lies actually, how they used. Along ++i(PreIncrement) the variable is incremented and new value is returned. Thus it needs one instruction to increment the va

Decode the Code, Smugglers are becoming very smart day by day. Now they hav...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

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