Beispiel virtuelle Basis

virtual.cpp
#include <iostream>
 
class DeepThought
{
public:
  int berechnen () 
  { // sleep(years2secs(7500000));
    return zahl << 1 ^ zahl << 3 ^ zahl << 5; 
  }
protected:
  int zahl;
  virtual ~DeepThought() {}
};
 
class Frage : virtual protected DeepThought
{
public:
  void stellen() { zahl = 1; }
};
 
class Antwort : virtual protected DeepThought
{
public:
  virtual int erfragen() { return berechnen(); }
};
 
class UltimativeAntwort : public Antwort, public Frage
{
};
 
int main()
{
  UltimativeAntwort antwort;
  Frage& frage = antwort;
  frage.stellen();
  std::cout << antwort.erfragen() << '\n';
  return 0;
}