Having created an array full of objects, you are almost ready to iterate through that array, displaying lines containing the cars' short names. First, however, you must choose how you want to store and display short names.
One approach is to include a member variable for short names in the
railroad_car
class. Next, you arrange for constructors in each
railroad-car subclass to write into that member variable the appropriate
character stringeng
, box
, tnk
, or cab
.
Finally, you can display the value of the member variable using the output
operator.
In the hardcopy version of this book, you learn how to declare member variables for character strings. In this chapter, you learn to include, in each railroad-car subclass, a member function that knows how to display that class's short name. Although not as simple as the member-variabledeclaration approach, the function-definition approach has a pedagogical benefit, because that approach exposes you to the need for C++'s soon-to-be-explained virtual functions.