Home Segments Index Top Previous Next

516: Mainline

Alternatively, instead of defining readers to expand access to the dimension member variables, you can expand access by moving the dimension declarations into what is called the protected part of the class 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;}
  protected: double height, width, length;  
}; 

Member variables and member functions in the protected part of a class definition are accessible only from member functions that are defined in the same class or in subclasses of that class. No access from any other function is allowed.