Advertisement
RobertDeMilo

WB3.12 Деструкторы

Sep 5th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 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.     }
  21.  
  22.     Route(const string& new_source, const string& new_destination)
  23.     {
  24.         source = new_source;
  25.         destination = new_destination;
  26.         UpdateLength();
  27.     }
  28.  
  29.     ~Route()
  30.     {
  31.         for (const string& entry : compute_distance_log)
  32.         {
  33.             cout << entry << "\n";
  34.         }
  35.     }
  36.  
  37.     string GetSource() const
  38.     {
  39.         return source;
  40.     }
  41.  
  42.     string GetDestination() const
  43.     {
  44.         return destination;
  45.     }
  46.  
  47.     int GetLength() const
  48.     {
  49.         return length;
  50.     }
  51.  
  52.     void SetSource(const string& new_source)
  53.     {
  54.         source = new_source;
  55.         UpdateLength();
  56.     }
  57.  
  58.     void SetDestination(const string& new_destination)
  59.     {
  60.         destination = new_destination;
  61.         UpdateLength();
  62.     }
  63.  
  64. private:
  65.  
  66.     void UpdateLength()
  67.     {
  68.         length = ComputeDistance(source, destination);
  69.         compute_distance_log.push_back(source + " " + destination);
  70.     }
  71.  
  72.     string source;
  73.     string destination;
  74.     int length;
  75.     vector<string> compute_distance_log;
  76.    
  77. };
  78.  
  79. void PrintRoute(const Route& route)
  80. {
  81.     cout << route.GetSource() << " - " <<
  82.         route.GetDestination() << "\n";
  83.  
  84. }
  85.  
  86. void ReverseRoute(Route& route)
  87. {
  88.     string old_source = route.GetSource();
  89.     string old_destination = route.GetDestination();
  90.     route.SetSource(old_destination);
  91.     route.SetDestination(old_source);
  92. }
  93.  
  94. int main()
  95. {
  96.     Route route("Moscow", "Saint Petersburg");
  97.     route.SetSource("Vyborg");
  98.     route.SetDestination("Vologda");
  99.  
  100.     return 0;
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement