Understanding friend classes and friend functions
Encapsulation is a valuable OOP feature that C++ offers through the proper usage of classes and access regions. Encapsulation offers uniformity in the manner in which data and behaviors are manipulated. In general, it is unwise to forfeit the encapsulated protection that a class offers.
There are, however, selected programming situations in which breaking encapsulation slightly is considered more acceptable than the alternative of providing an overly public interface to a class. That is, when a class needs to provide methods for two classes to cooperate, yet, in general, those methods are inappropriate to be publicly accessible.
Let’s consider a scenario that may lead us to consider slightly forfeiting (that is, breaking) the sacred OOP concept of encapsulation:
- Two tightly coupled classes may exist that are not otherwise related to one another. One class may have one or more associations with the other class and...