Home Segments Index Top Previous Next

470: Mainline

Suppose that you want to collect neither engine nor caboose objects in the train array. Instead, you want to display a message, Nonrevenue car type encountered. The following switch statement does the work with no duplication of the output statement:

switch (type_code) { 
  case 0: case 3: 
    cout << "Nonrevenue car type encountered" << endl; 
    break; 
  case 1: train[n] = new box_car;  break; 
  case 2: train[n] = new tank_car; break; 
}