friend Functions and friend Classes

The program of Figs. 2.15-2.16 (top) defines friend function setX to set the private data member x of class Count. Friend declaration can appear anywhere in the class. The program of Figs. 2.16 (bottom) -2.17 demonstrates the error messages produced by the compiler when nonfriend function cannotSetX is called to modify private data member x.
Figure 2.15: Friends can access private members of the class.
\includegraphics[scale=0.45]{figures//img7.33.ps}
\includegraphics[scale=0.45]{figures//img7.34.ps}
Figure 2.16: Nonfriend/nonmember functions cannot access private members. (part 1 of 2)
\includegraphics[scale=0.45]{figures//img7.35.ps}
\includegraphics[scale=0.45]{figures//img7.36.ps}
Figure 2.17: Nonfriend/nonmember functions cannot access private members. (part 2 of 2)
\includegraphics[scale=0.45]{figures//img7.37.ps}
\includegraphics[scale=0.45]{figures//img7.38.ps}
2004-07-29