Home Segments Index Top Previous Next

243: Mainline

Shadowing and explicit member function call are both illustrated in the following program:

#include 
int current_year = 2001;
const double pi = 3.14159;
// Container and railroad car class definitions go here
class box : public container {
  public: double height, width, length;
          // Default constructor:
          box ( ) { }
          // The box volume member function;
          double volume ( ) {return height * width * length;}        
};
// Cylinder definition goes here
class box_car : public railroad_car, public box {
  public: // Default constructor:
          box_car ( ) {height = 10.5; width = 9.2; length = 40.0;}
};
class gondola_car : public railroad_car, public box {
  public: // Default constructor:
          gondola_car ( ) {height = 6.0; width = 9.2; length = 40.0;}
          // The gondola volume member function;
          // gondola cars are loaded above their rims:
          double volume ( ) {return 1.2 * height * width * length;}  
}; 
// Other railroad car class definitions go here 
main ( ) { 
  // Construct a gondola car: 
  gondola_car g; 
  // Display volume; use the gondola class volume function: 
  cout << "Viewed as a gondola, the car's volume is " 
       << g.volume ( ) << " units." << endl; 
  // Display volume; use the box class volume function 
  cout << "Viewed as a box, the car's volume is " 
       << g.box::volume ( ) << " units." << endl; 
} 
--- Result --- 
Viewed as a gondola, the car's volume is 2649.6 units. 
Viewed as a box, the car's volume is 2208 units.