Home Segments Index Top Previous Next

192: Mainline

A writer is a function that inserts information into an object. One reason that you may wish to use a writer, rather than writing into a member variable directly, is that you can include additional computation in a writer. In Segment 189, you saw how to add a statement to the read_radius reader that announces each reference. The following provides the same enhancement to the write_radius writer:

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) {
      cout << "Writing a tank car's radius ..." << endl;  
      radius = r;} 
    double volume ( ) {return pi * radius * radius * length;} 
};