Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Base
  5. {
  6. void a()
  7. {
  8. cout << "a ";
  9. }
  10. void c()
  11. {
  12. cout << "c ";
  13. }
  14. void e()
  15. {
  16. cout << "e ";
  17. }
  18. // 2. Steps requiring peculiar implementations are "placeholders" in base class
  19. virtual void ph1() = 0;
  20. virtual void ph2() = 0;
  21. public:
  22. virtual ~Base() {}
  23. // 1. Standardize the skeleton of an algorithm in a base class "template method"
  24. void execute()
  25. {
  26. a();
  27. ph1();
  28. c();
  29. ph2();
  30. e();
  31. }
  32. };
  33.  
  34. class One: public Base
  35. {
  36. // 3. Derived classes implement placeholder methods
  37. /*virtual*/void ph1()
  38. {
  39. cout << "b ";
  40. }
  41. /*virtual*/void ph2()
  42. {
  43. cout << "d ";
  44. }
  45. };
  46.  
  47. class Two: public Base
  48. {
  49. /*virtual*/void ph1()
  50. {
  51. cout << "2 ";
  52. }
  53. /*virtual*/void ph2()
  54. {
  55. cout << "4 ";
  56. }
  57. };
  58.  
  59. int main()
  60. {
  61. Base *array[] =
  62. {
  63. new One(), new Two()
  64. };
  65. for (int i = 0; i < 2; i++)
  66. {
  67. array[i]->execute();
  68. cout << '\n';
  69. }
  70.  
  71. for (int i = 0; i < 2; i++)
  72. {
  73. delete array[i];
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement