fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using std::cout;
  6. using std::endl;
  7. using std::vector;
  8.  
  9. class Node
  10. {
  11. int _id;
  12. public:
  13. Node(int id) : _id(id)
  14. { }
  15. virtual int GetID() const { return _id; }
  16. };
  17.  
  18. class DNode : public Node
  19. {
  20. public:
  21. DNode(int id) : Node(id * 10)
  22. { }
  23. };
  24.  
  25. class Branch
  26. {
  27. vector<Node*> _list;
  28. public:
  29. void PrintList()
  30. {
  31. cout << endl << "Printing the contents of the list:" << endl << endl;
  32. for(vector<Node*>::const_iterator it = _list.begin(); it < _list.end(); it++)
  33. {
  34. cout << "Contained Node: ID " << (*it)->GetID() << "." << endl;
  35. }
  36. }
  37. Branch& operator <<(Node *op2)
  38. {
  39. cout << "Operator << triggered. Node ID: " << op2->GetID() << endl;
  40. cout << "Address received by operator <<: " << op2 << endl;
  41. _list.push_back(op2);
  42. if (dynamic_cast<DNode*>(op2)) cout << "(This is a derived object)" << endl;
  43. cout << endl;
  44. return *this;
  45. }
  46. };
  47.  
  48. int main()
  49. {
  50. Node *p;
  51. Branch B;
  52. cout << "Simple addition of 1 node:" << endl << endl;
  53. for(int i = 0; i < 10; i++)
  54. {
  55. p = new Node(i);
  56. cout << "Node's address: " << p << endl;
  57. B << p;
  58. }
  59. B.PrintList();
  60. cout << endl << "Chained addition:" << endl << endl;
  61. for(int i = 10; i < 20; i += 2)
  62. {
  63. p = new Node(i);
  64. Node *q = new DNode(i + 1);
  65. cout << "Node p's address: " << p << endl;
  66. cout << "Node q's address: " << q << endl;
  67. B << p << q;
  68. }
  69. B.PrintList();
  70. return 0;
  71. }
Success #stdin #stdout 0s 2860KB
stdin
Standard input is empty
stdout
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.