friend Functions and friend Classes

The program of Figs. 15-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. 16 (bottom) -17 demonstrates the error messages produced by the compiler when nonfriend function cannotSetX is called to modify private data member x.
Figure 15: Friends can access private members of the class.
\includegraphics[scale=0.45]{figures/img33.ps}
\includegraphics[scale=0.45]{figures/img34.ps}
Figure 16: Nonfriend/nonmember functions cannot access private members. (part 1 of 2)
\includegraphics[scale=0.45]{figures/img35.ps}
\includegraphics[scale=0.45]{figures/img36.ps}
Figure 17: Nonfriend/nonmember functions cannot access private members. (part 2 of 2)
\includegraphics[scale=0.45]{figures/img37.ps}
\includegraphics[scale=0.45]{figures/img38.ps}
2004-07-05