Home Segments Index Top Previous Next

198: Mainline

Suppose that you develop a big program around a tank_car class definition that includes readers and writers for radius and length member variables, as well as for an imaginary diameter 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;} 
    void write_radius (double r) {radius = r;} 
    double read_diameter ( ) {return 2.0 * radius;} 
    void write_diameter (double d) {radius = d / 2.0;} 
    double read_length ( ) {return length;} 
    void write_length (double l) {length = l;} 
    double volume ( ) {return pi * radius * radius * length;} 
}; 

Next, suppose that you discover that your program refers to diameters more often than radii. If speed is a great concern, you should arrange to store diameters, rather than radii, so as to avoid multiplication on reading and division on writing.