Guest User

Untitled

a guest
Feb 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. link-contructed
  2. Ca-contructed
  3. Cb-contructed
  4. Ca-run
  5. Cb-run
  6. Ca-destructed
  7. Cb-destructed
  8. link-destructed
  9.  
  10. /*
  11. vim: ts=4 :
  12. g++ -lstdc++ -o linked linked.cpp && ./linked
  13. */
  14.  
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. class Ctask {
  19. public:
  20. Ctask *next = NULL;
  21. Ctask(void) {}
  22. virtual ~Ctask(void) {}
  23. virtual void run(void) = 0;
  24. };
  25.  
  26. /* --------------------------------- */
  27.  
  28. class Clink {
  29. public:
  30. Clink(void) {
  31. cout << "link-contructed" << endl;
  32. }
  33. ~Clink(void) {
  34. Ctask *temp;
  35. while (temp = head) {
  36. head = head->next;
  37. delete temp;
  38. }
  39. cout << "link-destructed" << endl;
  40. }
  41. void run(void) {
  42. Ctask *temp = head;
  43. while (temp) {
  44. temp->run();
  45. temp = temp->next;
  46. }
  47. }
  48. void add(Ctask *newnode) {
  49. if (tail)
  50. tail->next = newnode;
  51. else
  52. head = newnode;
  53. tail = newnode;
  54. }
  55. private:
  56. Ctask *head = NULL;
  57. Ctask *tail = NULL;
  58. };
  59.  
  60. /* --------------------------------- */
  61.  
  62. class Ca: public Ctask {
  63. public:
  64. Ca(void) {
  65. cout << "Ca-contructed" << endl;
  66. }
  67. ~Ca(void) {
  68. cout << "Ca-destructed" << endl;
  69. }
  70. void run(void) {
  71. cout << "Ca-run" << endl;
  72. }
  73. };
  74.  
  75. class Cb: public Ctask {
  76. public:
  77. Cb(void) {
  78. cout << "Cb-contructed" << endl;
  79. }
  80. ~Cb(void) {
  81. cout << "Cb-destructed" << endl;
  82. }
  83. void run(void) {
  84. cout << "Cb-run" << endl;
  85. }
  86. };
  87.  
  88. /* --------------------------------- */
  89.  
  90. int main(void) {
  91. Clink link;
  92. link.add((Ctask *)new Ca());
  93. link.add((Ctask *)new Cb());
  94. link.run();
  95. return 0;
  96. }
Add Comment
Please, Sign In to add comment