Home Segments Index Top Previous Next

229: Mainline

As a general rule, you should place member variables and member functions in classes such that two criteria are satisfied:

For example, the percentage_loaded member variable is in the container class, because it is useful for all container subclasses, although it is not useful for classes that are not container subclasses, such as the engine and caboose classes. Similarly, the year_built member variable and the age member function are in the railroad_car class, because they are useful for all railroad cars, rather than just for those railroad cars that happen to be containers.

On the other hand, there are two volume member functions, because the way that you compute the volume of a box is different from the way that you compute the volume of a cylinder. Thus, there is duplication, but there is no needless duplication.