fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Base
  6. {
  7. public:
  8. virtual void Fun_1(){}
  9. virtual void Fun_2(){}
  10. virtual void Fun_3(){}
  11. };
  12.  
  13. class BaseComp : public Base
  14. {
  15. public:
  16. BaseComp():m_pBase(NULL){}
  17. BaseComp(Base* pBase):m_pBase(pBase){}
  18. virtual void Fun_1()
  19. {
  20. if(m_pBase)
  21. m_pBase->Fun_1();
  22. }
  23. virtual void Fun_2()
  24. {
  25. if(m_pBase)
  26. m_pBase->Fun_2();
  27. }
  28. virtual void Fun_3()
  29. {
  30. if(m_pBase)
  31. m_pBase->Fun_3();
  32. }
  33. protected:
  34. Base* m_pBase;
  35. };
  36. class Derive_1 : public BaseComp
  37. {
  38. public:
  39. Derive_1(){}
  40. Derive_1(Base* pBase):BaseComp(pBase){}
  41. virtual void Fun_1(){cout<<"Derive_1 Executed"<<endl;}
  42. };
  43.  
  44. class Derive_2 : public BaseComp
  45. {
  46. public:
  47. Derive_2(){}
  48. Derive_2(Base* pBase):BaseComp(pBase){}
  49. virtual void Fun_2(){cout<<"Derive_2 Executed"<<endl;}
  50. };
  51.  
  52. class Derive_3 : public BaseComp
  53. {
  54. public:
  55. Derive_3(){}
  56. Derive_3(Base* pBase):BaseComp(pBase){}
  57. virtual void Fun_3(){cout<<"Derive_3 Executed"<<endl;}
  58. };
  59.  
  60.  
  61. int main() {
  62.  
  63. Base* p1=new Derive_1(new Derive_2());
  64. cout<<"p1:"<<endl;
  65. p1->Fun_1();
  66. p1->Fun_2();
  67. p1->Fun_3();
  68.  
  69. cout<<endl<<"p2:"<<endl;
  70. Base* p2=new Derive_2(new Derive_3());
  71. p2->Fun_1();
  72. p2->Fun_2();
  73. p2->Fun_3();
  74.  
  75. cout<<endl<<"p3:"<<endl;
  76. Base* p3=new Derive_1(new Derive_3());
  77. p3->Fun_1();
  78. p3->Fun_2();
  79. p3->Fun_3();
  80.  
  81. cout<<endl<<"p4:"<<endl;
  82. Base* p4=new Derive_1(new Derive_3(new Derive_2()));
  83. p4->Fun_1();
  84. p4->Fun_2();
  85. p4->Fun_3();
  86. return 0;
  87. }
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
p1:
Derive_1 Executed
Derive_2 Executed

p2:
Derive_2 Executed
Derive_3 Executed

p3:
Derive_1 Executed
Derive_3 Executed

p4:
Derive_1 Executed
Derive_2 Executed
Derive_3 Executed