Source of vehicle_nv.hpp


  1: /** @file vehicle_nv.hpp

  2: Illustrates non-virtual inheritance.

  3: Based on an example from N. Josuttis, Object-Oriented Programming in C++.

  4: */
  5: 
  6: namespace Josuttis
  7: {
  8:     class Vehicle
  9:     {
 10:     protected:
 11:         int maxSpeed;
 12:     };
 13: 
 14:     class Car       : public Vehicle { };
 15:     class Boat      : public Vehicle { };
 16:     class Amphibian : public Car, public Boat
 17:     {
 18:     public:
 19:         void setAndShowInfo()
 20:         {
 21:             //The following code segment will not compile. Why not?

 22:             //maxSpeed = 100;  //or Amphibian::maxSpeed = 100;

 23:             //cout << maxSpeed << endl;

 24: 
 25:             Car::maxSpeed = 100;
 26:             cout << "\nMaximum land speed ..... " << Car::maxSpeed;
 27:             cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 28: 
 29:             Boat::maxSpeed = 70;
 30:             cout << "\nMaximum water speed .... " << Boat::maxSpeed;
 31:             cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 32: 
 33:             Vehicle::maxSpeed = 500;
 34:             //Change the order of the mulitple inheritance above to see how

 35:             //the output of this line is changed. What does this tell you?

 36:             cout << "\nMaximum vehicle speed .. " << Vehicle::maxSpeed;
 37:             cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 38: 
 39:             cout << "\nMaximum land speed ..... " << Car::maxSpeed;
 40:             cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 41: 
 42:             cout << "\nMaximum water speed .... " << Boat::maxSpeed;
 43:             cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 44:         }
 45:     };
 46: }