In Chapter 14, you learned that class definitions can have a private part as well as a public part. More specifically, you learned that you can access a class's private member variables only through member functions that belong to the same class.
If, for example, you decide that
a cylinder's dimensions never change once the cylinder is built, you can
reflect that constraint in the cylinder
class definition by moving
the dimensions out of the public interface into the
private part of the
definition:
class box {
public:
// Default constructor:
box ( ) { }
// Argument-bearing constructor:
box (double h, double w, double l) {
height = h; width = w; length = l;
}
// Volume member function:
double volume ( ) {return height * width * length;}
private: double height, width, length;
};
Given this box
class definition, only the argument-bearing
constructor defined in the box
class can write values into the three
dimension member variables. Similarly, only the volume
member
function can reference values in those member variables.
Because no other access to the member variables is allowed, no member
function of box_car
can write directly into or read directly from
the dimension member variables; all writing and reading must go through the
argument-bearing constructor and volume functions defined in the box
class's public interface.