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 C1 : public I1 {
  25. protected:
  26. int i;
  27. public:
  28. int getI() const { return i; }
  29. void setI(int i_) { i = i_; }
  30. };
  31.  
  32. class C12 : public I1, public I2 {
  33. protected:
  34. int i;
  35. string s;
  36. public:
  37. int getI() const { return i; }
  38. void setI(int i_) { i = i_; }
  39. string getS() const { return s; }
  40. void setS(string s_) { s = s_; }
  41. };
  42.  
  43. class C123 : public I1, public I2, public I3 {
  44. protected:
  45. int i;
  46. string s;
  47. float f;
  48. public:
  49. int getI() const { return i; }
  50. void setI(int i_) { i = i_; }
  51. string getS() const { return s; }
  52. void setS(string s_) { s = s_; }
  53. float getF() const { return f; }
  54. void setF(float f_) { f = f_; }
  55. };
  56.  
  57. template<class T>
  58. void f1(const T& c1)
  59. {
  60. cout << "f1:\n";
  61. cout << " getI: " << c1.getI() << endl;
  62. }
  63.  
  64. template<class T>
  65. void f2(const T& c12)
  66. {
  67. cout << "f2:\n";
  68. cout << " getI: " << c12.getI() << endl;
  69. cout << " getS: " << c12.getS() << endl;
  70. }
  71.  
  72. template<class T>
  73. void f3(const T& c23)
  74. {
  75. cout << "f3:\n";
  76. cout << " getS: " << c23.getS() << endl;
  77. cout << " getF: " << c23.getF() << endl;
  78. }
  79.  
  80. void test()
  81. {
  82. C1 c1;
  83. c1.setI(1);
  84. f1(c1);
  85.  
  86. cout << "\n===== " << endl;
  87.  
  88. C12 c12;
  89. c12.setI(12);
  90. c12.setS("str12");
  91. f1(c12);
  92. f2(c12);
  93.  
  94. cout << "\n===== " << endl;
  95.  
  96. C123 c123;
  97. c123.setI(123);
  98. c123.setF(1.23f);
  99. c123.setS("str123");
  100. f1(c123);
  101. f2(c123);
  102. f3(c123);
  103.  
  104. cout << "\n===== " << endl;
  105. }
  106.  
  107. int main()
  108. {
  109. test();
  110. }
  111.  
Success #stdin #stdout 0s 16072KB
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

=====