Advertisement
eitherlast

laba4.cpp

May 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <math.h>
  4. #include <string>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. class MyTime {
  10.  
  11.     private:
  12.  
  13.         int hours;
  14.         int minutes;
  15.  
  16.     public:
  17.  
  18.         MyTime()
  19.         {
  20.             hours = 12;
  21.             minutes = 0;
  22.         }
  23.  
  24.         MyTime(int H, int M)
  25.         {
  26.             Set(H, M);
  27.         }
  28.  
  29.         MyTime(int M)
  30.         {
  31.             hours = M / 60 % 24;
  32.             minutes = M % 60;
  33.             FixTime();
  34.         }
  35.  
  36.         void Set(int H, int M)
  37.         {
  38.             hours = H;
  39.             minutes = M;
  40.             FixTime();
  41.         }
  42.  
  43.         void FixTime()
  44.         {
  45.             minutes < 60 ? minutes = abs(minutes) : (hours += abs(minutes) / 60) && (minutes = abs(minutes) % 60);
  46.             hours = abs(hours % 24);
  47.         }
  48.  
  49.         virtual void Show()
  50.         {
  51.             cout << hours << " часов и " << minutes << " минут. \n";
  52.         }
  53.  
  54.  
  55.         MyTime Sum(MyTime t2)
  56.         {
  57.             MyTime t;
  58.             t.Set(hours + t2.hours, minutes + t2.minutes);
  59.             return t;
  60.         }
  61.  
  62.  
  63.         friend ostream& operator << (ostream & os, const MyTime &);
  64.  
  65.         virtual MyTime operator + (const MyTime & t2)
  66.         {
  67.             return MyTime(hours + t2.hours, minutes + t2.minutes);
  68.         }
  69.  
  70.         MyTime operator ++ (int)
  71.         {
  72.             MyTime t = MyTime(this->hours, this->minutes++);
  73.             FixTime();
  74.             return t;
  75.         }
  76.  
  77.  
  78.         virtual ~MyTime()
  79.         {
  80.             cout << "Удаляется объект базового класса. \n";
  81.         };
  82. };
  83.  
  84.  
  85. class CityTime : public MyTime
  86. {
  87.     private:
  88.         string city;
  89.     public:
  90.         CityTime(int H, int M, string City) : MyTime(H, M), city (City) {}
  91.  
  92.     void Set(int H, int M, int City)
  93.     {
  94.         MyTime::Set(H, M);
  95.         city = City;
  96.         FixCityName(city);
  97.     }
  98.  
  99.     void Show()
  100.     {
  101.         FixCityName(city);
  102.         cout << "В городе " << city << " ";
  103.         MyTime::Show();
  104.     }
  105.  
  106.     string FixCityName (string &City)
  107.     {
  108.         City[0] = toupper(City[0]);
  109.         return City;
  110.     }
  111.  
  112.     CityTime operator + (const CityTime & t2)
  113.     {
  114.         return CityTime(hours + t2.hours, minutes + t2.minutes);
  115.     }
  116.  
  117.     ~CityTime()
  118.     {
  119.         cout << "Удаляется объект класса-наследника. \n";
  120.     };
  121. };
  122.  
  123. ostream & operator << (ostream & os, const MyTime & t) // why & ?
  124. {
  125.     os << "\nIt's " << t.hours << " hours and " << t.minutes << " minutes." << endl;
  126.     return os;
  127. }
  128.  
  129. int main()
  130. {
  131.     setlocale(LC_ALL, "rus");
  132.     srand(time(NULL));
  133.  
  134.     CityTime MoscowTime = CityTime(18, 22, "москва");
  135.     MoscowTime.Show();
  136.  
  137.     string cityArr[] = { "москва", "Лондон", "шанхай", "Вашингтон", "Дублин" };
  138.     MyTime** timeArr = new MyTime* [10];
  139.     for (int i = 0; i < 10; i++)
  140.     {
  141.         if (i % 2 == 0)
  142.         {
  143.             timeArr[i] = new MyTime(rand(), rand());
  144.             timeArr[i]->Show();
  145.         }
  146.         else
  147.         {
  148.             timeArr[i] = new CityTime(rand(), rand(), cityArr[i / 2]);
  149.             timeArr[i]->Show();
  150.         }
  151.     }
  152.    
  153.     for (int i = 0; i < 10; i++) {
  154.         delete timeArr[i];
  155.     }
  156.  
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement