Home Segments Index Top Previous Next

190: Mainline

You may also wish to use readers to provide access to imaginary member variables that exist only in the sense that their values can be computed from member variables that do exist. For example, you can create read_diameter, which seems to refer to the contents of an imaginary diameter member variable, but which actually refers to the contents of the radius member variable:

class tank_car {
  public:
    double radius, length;
    tank_car ( ) {radius = 3.5; length = 40.0;}
    tank_car (double r, double l) {radius = r; length = l;}
    double read_radius ( ) {return radius;}
    double read_diameter ( ) {return radius * 2.0;}  
    double volume ( ) {return pi * radius * radius * length;} 
};