The version of the analyze_train
shown in the program in
Segment 460 contains a sequence of if-else
statements
that determines what to do for any given value of the type_code
variable:
if (type_code == 0) train[n] = new engine; else if (type_code == 1) train[n] = new box_car; else if (type_code == 2) train[n] = new tank_car; else if (type_code == 3) train[n] = new caboose;
Most C++ programmers use a switch
statement instead:
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; }