Home Segments Index Top Previous Next

480: Mainline

At this point, you have seen a switch statement that analyzes type codes found in a sensor file and that creates appropriate objects:

switch (type_code) { 
  case 0: train[n] = new engine;   break; 
  case 1: train[n] = new box_car;  break; 
  case 2: train[n] = new tank_car; break; 
  case 3: train[n] = new caboose;  break; 
} 

Although this switch statement works fine, it is difficult to remember which number goes with which car type. In general, it is difficult to maintain a program that is littered with such nonmnemonic encoding.