fork download
  1. #include <iostream>
  2.  
  3. /*
  4.   A
  5.   / \
  6.   B C
  7.   \ /
  8.   D
  9. */
  10.  
  11. using namespace std;
  12.  
  13. class A
  14. {
  15. public:
  16. virtual string whoami() const
  17. {
  18. return "A";
  19. }
  20. };
  21.  
  22. class B : public A
  23. {
  24. public:
  25. virtual string whoami() const override
  26. {
  27. return "B";
  28. }
  29. };
  30.  
  31. class C : public A
  32. {
  33. public:
  34. virtual string whoami() const override
  35. {
  36. return "C";
  37. }
  38. };
  39.  
  40. class D : public B, public C
  41. {
  42. public:
  43. virtual string whoami() const override
  44. {
  45. return "D";
  46. }
  47. };
  48.  
  49. int main()
  50. {
  51. D *d = new D;
  52. B *b = d;
  53. C *c = d;
  54. A *b2a = b;
  55. A *c2a = c;
  56.  
  57. cout << d->whoami() << endl;
  58. cout << b->whoami() << endl;
  59. cout << c->whoami() << endl;
  60. cout << b2a->whoami() << endl;
  61. cout << c2a->whoami() << endl;
  62.  
  63. delete d;
  64. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
D
D
D
D
D