Plainly, movement of the dimension member variables into the private part
of the box
class definition is an extreme step. If you want to take
a slightly less extreme step, allowing you to read the dimensions
elsewhere, but not to write them, you can add readers to the public
interface:
class box { public: // Define readers: double read_height ( ) {return height;} double read_width ( ) {return width;} double read_length ( ) {return length;} // 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; };
Because the readers are defined in the box
class, they have access
to the dimension member variables declared in the private part of the
box
class. Because the readers themselves are defined in the public
part of the definition, they are accessible everywhere. Thus, if you use
the publicly defined readers, any member function or ordinary function can
read a box car's dimension member variables, but only those member
functions defined in the box-car class can write into those dimension
member variables.