Home Segments Index Top Previous Next

548: Mainline

Suppose that you decide to define an ordinary function—not a member function—that takes an ordinary railroad-car argument and computes that car's volume. You might try to define ordinary_capacity_function as an ordinary function by having it give all the work to the already-defined capacity virtual member function:

// DEFECTIVE attempt to define ordinary_capacity_function! 
double ordinary_capacity_function (railroad_car r) { 
  return r.capacity ( ); 

You might think that this function would work properly in the analyze_train program if called as follows:

for (n = 0; n < car_count; ++n) { 
 // Display short name and capacity and terminate the line:  
 cout << train[n] -> short_name ( ) 
      << "     " 
      << ordinary_capacity_function (*train[n]) 
      << endl;                       --------- 
}                                        ^ 
Dereferenced array pointer identifies  --* 
a chunk of memory that holds a box car, 
tank car, engine, or caboose 

Perhaps unexpectedly, incorporating this version of ordinary_capacity_function into the analyze_train program leads to the following result:

eng     0 
box     0 
box     0 
tnk     0 
cab     0