Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- link-contructed
- Ca-contructed
- Cb-contructed
- Ca-run
- Cb-run
- Ca-destructed
- Cb-destructed
- link-destructed
- /*
- vim: ts=4 :
- g++ -lstdc++ -o linked linked.cpp && ./linked
- */
- #include <iostream>
- using namespace std;
- class Ctask {
- public:
- Ctask *next = NULL;
- Ctask(void) {}
- virtual ~Ctask(void) {}
- virtual void run(void) = 0;
- };
- /* --------------------------------- */
- class Clink {
- public:
- Clink(void) {
- cout << "link-contructed" << endl;
- }
- ~Clink(void) {
- Ctask *temp;
- while (temp = head) {
- head = head->next;
- delete temp;
- }
- cout << "link-destructed" << endl;
- }
- void run(void) {
- Ctask *temp = head;
- while (temp) {
- temp->run();
- temp = temp->next;
- }
- }
- void add(Ctask *newnode) {
- if (tail)
- tail->next = newnode;
- else
- head = newnode;
- tail = newnode;
- }
- private:
- Ctask *head = NULL;
- Ctask *tail = NULL;
- };
- /* --------------------------------- */
- class Ca: public Ctask {
- public:
- Ca(void) {
- cout << "Ca-contructed" << endl;
- }
- ~Ca(void) {
- cout << "Ca-destructed" << endl;
- }
- void run(void) {
- cout << "Ca-run" << endl;
- }
- };
- class Cb: public Ctask {
- public:
- Cb(void) {
- cout << "Cb-contructed" << endl;
- }
- ~Cb(void) {
- cout << "Cb-destructed" << endl;
- }
- void run(void) {
- cout << "Cb-run" << endl;
- }
- };
- /* --------------------------------- */
- int main(void) {
- Clink link;
- link.add((Ctask *)new Ca());
- link.add((Ctask *)new Cb());
- link.run();
- return 0;
- }
Add Comment
Please, Sign In to add comment