However shouldn''t i always use a printon() method instead , C/C++ Programming

Assignment Help:

Q: however shouldn't I always use a printOn() method instead of a friend function?

A: No.

The usual cause people wish to always use a printOn() method instead of  a friend function is since they wrongly believe that friends violate encapsulation and/or that friends are wicked. These beliefs are wrong and naive: while used properly, friends can in fact enhance encapsulation.

It is not to say that the printOn() method approach is never useful. For instance, it is useful while providing printing for whole hierarchy of classes. However if you employ a printOn() method, normally it should be protected, not public.

For wholeness, here is "the printOn() method approach." The proposal is to contain a member function, frequently called printOn(), that does the definite printing, then have operator<< call that printOn() method. While it is done wrongly, the printOn() method is public so operator<< doesn't ought to be a friend it can be a simple top-level function which is neither a friend nor a member of the class. some sample code following:

#include class Fred {

public:

void printOn(std::ostream& o) const;

...

};

 

// operator<< can be declared as a non-friend [NOT recommended!]

std::ostream& operator<< (std::ostream& o, const Fred& fred);

// The real printing is done inside printOn() method [NOT recommended!]

void Fred::printOn(std::ostream& o) const

{

...

}

// operator<< calls printOn() [NOT recommended!]

std::ostream& operator<< (std::ostream& o, const Fred& fred)

{ fred.printOn(o); return o;

}

People assume wrongly that this decrease maintenance cost "since it ignore having a friend function." It is a incorrect supposition because the following:

The member-called-by-top-level-function approach contains zero benefit in terms of maintenance cost. Let's say this takes N lines of code to do the real printing. In the case of a friend function, those N lines of code will contain direct access to the class's private/protected parts which means whenever someone modify the class's private/protected parts, those N lines of code will have to be scanned & possibly modified, which enhance the maintenance cost. Though using the printOn() method doesn't modify this at all: we still contain N lines of code which have direct access to the class's private/protected parts. Therefore moving the code from a friend function in a member function does not decrease the maintenance cost at all. Zero reduction. No advantage in maintenance cost. (If anything it's a bit worse along with the printOn() method as now you have more lines of code to maintain as you have an extra function that you didn't contain before.)

The member-called-by-top-level-function approach makes the class difficult to use, specifically by programmers who are not also class designers. The approach exposes public method that programmers are not imagined calling. While a programmer reads the public methods of the class, they'll notice two ways to do the similar thing. The documentation would have to say something like, "precisely it does the similar as that though don't use this; instead use that." & the average programmer will say, "Why make the method public if I'm not likely to use it?" In reality just one cause the printOn() method is public is to ignore granting friendship status to operator<<, and that is a notion i.e. somewhere among subtle and incomprehensible to a programmer who simply wished to use the class.

Net: the member-called-by-top-level-function approach has a cost however no benefit. Thus it is, in general, a bad idea.

Note: if the printOn() method is private or protected, the second objection doesn't apply. There are cases while that approach is reasonable, such as while providing printing for an whole hierarchy of classes. Note down also that while the printOn() method is non-public, operator<< needs to be a friend.

 


Related Discussions:- However shouldn''t i always use a printon() method instead

., what is polymorphism

what is polymorphism

C program coding, write a c program to compute a padovan string

write a c program to compute a padovan string

Area under the curve, Write a program to find the area under the curve y = ...

Write 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 two points can b

Tell what the derived class inherits or doesn''t inherit?, Tell what the de...

Tell what the derived class inherits or doesn't inherit? A: Inherits: - All data member defined in the parent class (even though such members might not always be accessible

List, Is there any difference between List x; and List x ()? Can you please...

Is there any difference between List x; and List x ()? Can you please provide me assignment help.

#psuedocode, Create a pseudocode in getting Calendar Quarter. The program s...

Create a pseudocode in getting Calendar Quarter. The program should identify which quarter falls the given date. Note: Consider the date format DDMMYYYY.

Write the statements which are used to connect php, Write the statements wh...

Write the statements which are used to connect PHP with MySQL? Statements which can be used to connect PHP with MySQL is: $conn = mysql_connect('localhost'); echo $co

Addition of array elements, Addition of array elements: #define rows 3 ...

Addition of array elements: #define rows 3 #define cols 3 void main() {                 int i=0,j=0,sum=0;                 int arr[rows][cols];

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