Virtual base classes : base class « Class « C++ Tutorial






#include <iostream>
using namespace std;

class base {
public:
  int i;
};

class derived1 : virtual public base {
public:
  int j;
};

class derived2 : virtual public base {
public:
  int k;
};

/* derived3 inherits both derived1 and derived2.
   This time, there is only one copy of base class. */
class derived3 : public derived1, public derived2 {
public:
  int sum;
};

int main()
{
  derived3 ob;

  ob.i = 10; // now unambiguous
  return 0;
}








9.17.base class
9.17.1.Add a constructor to Base class
9.17.2.Add a overloaded constructor to base class
9.17.3.Call base's default constructor automatically
9.17.4.Call base virtual function explicitly
9.17.5.Virtual base classes
9.17.6.Pass parameter to two base classes