Programming and Graphics

(Kiana) #1

6.4 Private fields, public fields, and global variables 159


class fruit
{
public:
fruit();
{
q=dvq;
w=dvw;

e=dve;
}
fruit(q, w, ..., e);

private:

};

However, this layout obscures the class structure in the absence of a concise
class definition. It is thus highly recommended that class definition and class
implementation are put in separate sections.


Problems


6.3.1.Explain why it does not make practical sense to define private construc-
tors.


6.3.2.An integer variable is declared as:


int a;

Is this statement consistent with a default constructor?

6.4 Private fields, public fields, and global variables..........


Next, we discuss the “private” variables of a class. To understand this concept,
it is helpful to imagine that a class is a biological cell or capsule whose interior
can be accessed, probed, altered or destroyed only by the member (capsule)
functions. The capsule encloses data which, if declared “private,” can be ac-
cessed only by the member functions of the host class, but not by any other
functions.


For example, if the string variablecolor, the string variableshape,and
the real variablesizeare private variables of the “fruit” class, we define:


class fruit
{
Free download pdf