fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class I1 {
  7. public:
  8. virtual int getI() const = 0;
  9. virtual void setI(int i) = 0;
  10. };
  11.  
  12. class I2 {
  13. public:
  14. virtual string getS() const = 0;
  15. virtual void setS(string s) = 0;
  16. };
  17.  
  18. class I3 {
  19. public:
  20. virtual float getF() const = 0;
  21. virtual void setF(float f) = 0;
  22. };
  23.  
  24. class I12 : virtual public I1, virtual public I2 {};
  25. class I23 : virtual public I2, virtual public I3 {};
  26. class I123 : virtual public I12, virtual public I23 {};
  27.  
  28. class C1 : virtual public I1 {
  29. protected:
  30. int i;
  31. public:
  32. int getI() const { return i; }
  33. void setI(int i_) { i = i_; }
  34. };
  35.  
  36. class C2 : virtual public I2 {
  37. protected:
  38. string s;
  39. public:
  40. string getS() const { return s; }
  41. void setS(string s_) { s = s_; }
  42. };
  43.  
  44. class C3 : virtual public I3 {
  45. protected:
  46. float f;
  47. public:
  48. float getF() const { return f; }
  49. void setF(float f_) { f = f_; }
  50. };
  51.  
  52. class C12 : public I12, public C1, public C2 {};
  53. class C123 : public I123, public C1, public C2, public C3 {};
  54.  
  55. void f1(const I1& c1)
  56. {
  57. cout << "f1:\n";
  58. cout << " getI: " << c1.getI() << endl;
  59. }
  60.  
  61. void f2(const I12& c12)
  62. {
  63. cout << "f2:\n";
  64. cout << " getI: " << c12.getI() << endl;
  65. cout << " getS: " << c12.getS() << endl;
  66. }
  67.  
  68. void f3(const I123& c23)
  69. {
  70. cout << "f3:\n";
  71. cout << " getS: " << c23.getS() << endl;
  72. cout << " getF: " << c23.getF() << endl;
  73. }
  74.  
  75. void test()
  76. {
  77. C1 c1;
  78. c1.setI(1);
  79. f1(c1);
  80.  
  81. cout << "\n===== " << endl;
  82.  
  83. C12 c12;
  84. c12.setI(12);
  85. c12.setS("str12");
  86. f1(c12);
  87. f2(c12);
  88.  
  89. cout << "\n===== " << endl;
  90.  
  91. C123 c123;
  92. c123.setI(123);
  93. c123.setF(1.23f);
  94. c123.setS("str123");
  95. f1(c123);
  96. f2(c123);
  97. f3(c123);
  98.  
  99. cout << "\n===== " << endl;
  100. }
  101.  
  102. int main()
  103. {
  104. test();
  105. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
f1:
  getI: 1

===== 
f1:
  getI: 12
f2:
  getI: 12
  getS: str12

===== 
f1:
  getI: 123
f2:
  getI: 123
  getS: str123
f3:
  getS: str123
  getF: 1.23

=====