Advertisement
RobertDeMilo

WB3.13 Время жизни объекта

Sep 5th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int ComputeDistance(const string& source, const string& destination)
  7. {
  8.     return source.length() - destination.length();
  9. }
  10.  
  11. class Route
  12. {
  13. public:
  14.  
  15.     Route()
  16.     {
  17.         source = "Moscow";
  18.         destination = "Saint Petersburg";
  19.         UpdateLength();
  20.         cout << "Default constructed\n";
  21.     }
  22.  
  23.     Route(const string& new_source, const string& new_destination)
  24.     {
  25.         source = new_source;
  26.         destination = new_destination;
  27.         UpdateLength();
  28.         cout << "Constructed\n";
  29.     }
  30.  
  31.     ~Route()
  32.     {
  33.         cout << "Destructed\n";
  34.     }
  35.  
  36.     string GetSource() const
  37.     {
  38.         return source;
  39.     }
  40.  
  41.     string GetDestination() const
  42.     {
  43.         return destination;
  44.     }
  45.  
  46.     int GetLength() const
  47.     {
  48.         return length;
  49.     }
  50.  
  51.     void SetSource(const string& new_source)
  52.     {
  53.         source = new_source;
  54.         UpdateLength();
  55.     }
  56.  
  57.     void SetDestination(const string& new_destination)
  58.     {
  59.         destination = new_destination;
  60.         UpdateLength();
  61.     }
  62.  
  63. private:
  64.  
  65.     void UpdateLength()
  66.     {
  67.         length = ComputeDistance(source, destination);
  68.     }
  69.  
  70.     string source;
  71.     string destination;
  72.     int length;
  73. };
  74.  
  75. void Worthless(Route route)
  76. {
  77.     cout << 2 << "\n";
  78. }
  79.  
  80. Route GetRoute()
  81. {
  82.     cout << 1 << "\n";
  83.     return {};  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   возвращаем маршрут, созданный при помощи конструктора по умолчанию
  84. }
  85.  
  86. int main()
  87. {
  88.     ********************************************************
  89.     cout << 1 << "\n";
  90.     Route route;
  91.     cout << 2 << "\n";
  92.     ********************************************************
  93.     for (int i : {0, 1})                    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  94.     {
  95.         cout << "Step " << i << ": " << 1 << "\n";
  96.         Route route;
  97.         cout << "Step " << i << ": " << 2 << "\n";
  98.     }
  99.     cout << "End\n";
  100.     ********************************************************
  101.     cout << 1 << "\n";
  102.     Route first_route;
  103.     if (false)
  104.     {
  105.         cout << 2 << "\n";
  106.         return 0;
  107.     }
  108.     cout << 3 << "\n";
  109.     Route second_route;
  110.     cout << 4 << "\n";
  111.     ********************************************************
  112.     cout << 1 << "\n";
  113.     Worthless({});  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!  маршрут, созданный с помощью конструктора по умолчанию {}
  114.     cout << 3 << "\n";
  115.     ********************************************************
  116.     Route route = GetRoute();     !!!!!!!!!!!!!!!!!!!!!!!!!
  117.     cout << 2 << "\n";            !!!!!!!!!!!!!!!!!!!!!!!!!
  118.     1
  119.     Default constructed
  120.     2
  121.     Destructed
  122.     ********************************************************
  123.     GetRoute();                   !!!!!!!!!!!!!!!!!!!!!!!!!
  124.     cout << 2 << "\n";            !!!!!!!!!!!!!!!!!!!!!!!!!
  125.        
  126.     return 0;
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement