Source of vehicle_v.hpp


  1: /** @file vehicle_v.hpp

  2: Illustrates virtual inheritance from a "virtual base class".

  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 yearOfManufacture;
 12:     };
 13: 
 14:     class Car  : virtual public Vehicle { }; //Note different qualifier order.

 15:     class Boat : public virtual Vehicle { }; //So, order doesn't matter.

 16:     class Amphibian : public Car, public Boat
 17:     {
 18:     public:
 19:         void setAndShowInfo()
 20:         {
 21:             yearOfManufacture = 1995;
 22:             cout << endl;
 23:             cout << yearOfManufacture << endl;
 24:             cout << Car::yearOfManufacture << endl;
 25:             cout << Boat::yearOfManufacture << endl;
 26:             cout << Vehicle::yearOfManufacture << endl;
 27:             cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 28:         }
 29:     };
 30: }