6.6 Friends 163
Running this program prints on the screen:
green
green
red
yellow
Because the attributecolorhas been declared as private, we cannot state
in the main program:
cout << fig.color << endl;
This would be acceptable only if the declaration:
string color;
were made in the public section of the class.
Problems
6.5.1.Add to the fruit class a member function that prints and returns (a)the
shape, and (b) the size of an object.
6.5.2.Add to the fruit class a member function that changes all three attributes
of an object.
6.5.3.Introduce a global variable of your choice and confirm that it can be
initialized and evaluated inside the main program or any fruit function.
6.5.4.Define the class of all taxpayers whose attributes include last name, first
name, social-security number, and income.
6.6 Friends.................................
Privacy exceptions can be made to friends. If we want to disclose the private
fields of the class members to an external function namedpackage, we state
this in the class definition. In the case of the fruit class, we state:
//--- CLASS FRUIT DEFINITION
class fruit
{
friend void package(fruit item);
public:
fruit();
fruit(string color, string shape, float size);