fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // Part B
  9. struct Room
  10. {
  11. int d_noSeat;
  12. bool d_hasProjector;
  13.  
  14. Room() = default;
  15.  
  16. Room(const Room& r) :
  17. d_noSeat(r.d_noSeat),
  18. d_hasProjector(r.d_hasProjector)
  19. {}
  20. };
  21.  
  22.  
  23. class Event
  24. {
  25. std::shared_ptr<Room> d_room;
  26. std::string d_name;
  27. public:
  28. Event() :
  29. d_room(0),
  30. d_name("")
  31. {}
  32.  
  33. Event(Room& r, const std::string& name):
  34. d_room(new Room(r)),
  35. d_name(name)
  36. {}
  37.  
  38. Event(const Event& e) :
  39. d_room(new Room(*e.d_room)),
  40. d_name(e.d_name)
  41. {}
  42.  
  43. void print()
  44. {
  45. std::cout << "Event: " << d_name;
  46. if (d_room != 0)
  47. {
  48. std::cout << " in size " << d_room->d_noSeat;
  49. if (d_room->d_hasProjector)
  50. std::cout << " with";
  51. else
  52. std::cout << " without";
  53. std::cout << " projector";
  54. }
  55. std::cout << std::endl;
  56. return;
  57. }
  58.  
  59. ~Event() {}
  60. };
  61.  
  62.  
  63.  
  64. int main()
  65. {
  66. const int noLect = 5;
  67. Room r;
  68. std::vector<Event> lectures;
  69.  
  70. for (int i = 0; i < noLect; ++i)
  71. {
  72. r.d_noSeat = i + 1;
  73. r.d_hasProjector = !r.d_hasProjector;
  74. lectures.emplace_back(r, "CSI2372");
  75. lectures[i].print();
  76. }
  77.  
  78. std::cout << "-------------------" << std::endl;
  79. for (int i = 0; i < noLect; ++i)
  80. {
  81. lectures[i].print();
  82. }
  83. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Event: CSI2372 in size 1 without projector
Event: CSI2372 in size 2 with projector
Event: CSI2372 in size 3 without projector
Event: CSI2372 in size 4 with projector
Event: CSI2372 in size 5 without projector
-------------------
Event: CSI2372 in size 1 without projector
Event: CSI2372 in size 2 with projector
Event: CSI2372 in size 3 without projector
Event: CSI2372 in size 4 with projector
Event: CSI2372 in size 5 without projector