fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class WithNotification {
  6. public:
  7. virtual void notify()=0;
  8. };
  9.  
  10. class Robot : public virtual WithNotification {
  11. private:
  12. string name;
  13. public:
  14. Robot(const string& n) : name(n) {}
  15. virtual void notify() {cout << name << " has been notified" << endl; }
  16. };
  17.  
  18. class Task {
  19. private:
  20. WithNotification& onFinished;
  21. public:
  22. Task(WithNotification& f) : onFinished(f) {}
  23. void run() {
  24. cout << "The task is running" << endl;
  25. onFinished.notify();
  26. }
  27. };
  28.  
  29. int main() {
  30. Robot r1("Quick");
  31. Robot r2("Brown");
  32. Task t1(r1);
  33. Task t2(r2);
  34. t1.run();
  35. t2.run();
  36. }
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
The task is running
Quick has been notified
The task is running
Brown has been notified