#include <iostream> #include <vector> #include <string> using std::cout; using std::endl; using std::vector; class Node { int _id; public: Node(int id) : _id(id) { } virtual int GetID() const { return _id; } }; class DNode : public Node { public: DNode(int id) : Node(id * 10) { } }; class Branch { vector<Node*> _list; public: void PrintList() { cout << endl << "Printing the contents of the list:" << endl << endl; for(vector<Node*>::const_iterator it = _list.begin(); it < _list.end(); it++) { cout << "Contained Node: ID " << (*it)->GetID() << "." << endl; } } Branch& operator <<(Node *op2) { cout << "Operator << triggered. Node ID: " << op2->GetID() << endl; cout << "Address received by operator <<: " << op2 << endl; _list.push_back(op2); if (dynamic_cast<DNode*>(op2)) cout << "(This is a derived object)" << endl; cout << endl; return *this; } }; int main() { Node *p; Branch B; cout << "Simple addition of 1 node:" << endl << endl; for(int i = 0; i < 10; i++) { p = new Node(i); cout << "Node's address: " << p << endl; B << p; } B.PrintList(); cout << endl << "Chained addition:" << endl << endl; for(int i = 10; i < 20; i += 2) { p = new Node(i); Node *q = new DNode(i + 1); cout << "Node p's address: " << p << endl; cout << "Node q's address: " << q << endl; B << p << q; } B.PrintList(); return 0; }
Standard input is empty
Simple addition of 1 node: Node's address: 0x8e49008 Operator << triggered. Node ID: 0 Address received by operator <<: 0x8e49008 Node's address: 0x8e49028 Operator << triggered. Node ID: 1 Address received by operator <<: 0x8e49028 Node's address: 0x8e49018 Operator << triggered. Node ID: 2 Address received by operator <<: 0x8e49018 Node's address: 0x8e49038 Operator << triggered. Node ID: 3 Address received by operator <<: 0x8e49038 Node's address: 0x8e49060 Operator << triggered. Node ID: 4 Address received by operator <<: 0x8e49060 Node's address: 0x8e49098 Operator << triggered. Node ID: 5 Address received by operator <<: 0x8e49098 Node's address: 0x8e490a8 Operator << triggered. Node ID: 6 Address received by operator <<: 0x8e490a8 Node's address: 0x8e490b8 Operator << triggered. Node ID: 7 Address received by operator <<: 0x8e490b8 Node's address: 0x8e490c8 Operator << triggered. Node ID: 8 Address received by operator <<: 0x8e490c8 Node's address: 0x8e49120 Operator << triggered. Node ID: 9 Address received by operator <<: 0x8e49120 Printing the contents of the list: Contained Node: ID 0. Contained Node: ID 1. Contained Node: ID 2. Contained Node: ID 3. Contained Node: ID 4. Contained Node: ID 5. Contained Node: ID 6. Contained Node: ID 7. Contained Node: ID 8. Contained Node: ID 9. Chained addition: Node p's address: 0x8e49130 Node q's address: 0x8e49140 Operator << triggered. Node ID: 10 Address received by operator <<: 0x8e49130 Operator << triggered. Node ID: 110 Address received by operator <<: 0x8e49140 (This is a derived object) Node p's address: 0x8e49150 Node q's address: 0x8e49160 Operator << triggered. Node ID: 12 Address received by operator <<: 0x8e49150 Operator << triggered. Node ID: 130 Address received by operator <<: 0x8e49160 (This is a derived object) Node p's address: 0x8e49170 Node q's address: 0x8e49180 Operator << triggered. Node ID: 14 Address received by operator <<: 0x8e49170 Operator << triggered. Node ID: 150 Address received by operator <<: 0x8e49180 (This is a derived object) Node p's address: 0x8e49190 Node q's address: 0x8e491a0 Operator << triggered. Node ID: 16 Address received by operator <<: 0x8e49190 Operator << triggered. Node ID: 170 Address received by operator <<: 0x8e491a0 (This is a derived object) Node p's address: 0x8e49238 Node q's address: 0x8e49248 Operator << triggered. Node ID: 18 Address received by operator <<: 0x8e49238 Operator << triggered. Node ID: 190 Address received by operator <<: 0x8e49248 (This is a derived object) Printing the contents of the list: Contained Node: ID 0. Contained Node: ID 1. Contained Node: ID 2. Contained Node: ID 3. Contained Node: ID 4. Contained Node: ID 5. Contained Node: ID 6. Contained Node: ID 7. Contained Node: ID 8. Contained Node: ID 9. Contained Node: ID 10. Contained Node: ID 110. Contained Node: ID 12. Contained Node: ID 130. Contained Node: ID 14. Contained Node: ID 150. Contained Node: ID 16. Contained Node: ID 170. Contained Node: ID 18. Contained Node: ID 190.