Home Segments Index Top Previous Next

193: Mainline

You may also wish to use writers that pretend to write into imaginary member variables, but that actually dictate values for member variables that do exist. For example, you can create a write_diameter member function, which seems to write into an imaginary diameter member variable, but which actually writes into 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;}
    void write_radius (double r) {radius = r;}
    void write_diameter (double d) {radius = d / 2.0;}  
    double volume ( ) {return pi * radius * radius * length;} 
};