Advertisement
eitherlast

laba4.01.cpp

May 17th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 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.     protected:
  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.         virtual 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.             cout << "qq";
  68.             return MyTime(hours + t2.hours, minutes + t2.minutes);
  69.         }
  70.    
  71.         MyTime operator ++ (int)
  72.         {
  73.             MyTime t = MyTime(this->hours, this->minutes++);
  74.             FixTime();
  75.             return t;
  76.         }
  77.  
  78.  
  79.  
  80.         virtual ~MyTime()
  81.         {
  82.             cout << "Удаляется объект базового класса. \n";
  83.         };
  84. };
  85.  
  86.  
  87. class CityTime : public MyTime
  88. {
  89.     private:
  90.         string city;
  91.     public:
  92.         CityTime(int H, int M, string City) : MyTime(H, M), city (City) {}
  93.  
  94.     void Set(int H, int M, string City)
  95.     {
  96.         MyTime::Set(H, M);
  97.         city = City;
  98.         FixCityName(city);
  99.     }
  100.  
  101.     void Show()
  102.     {
  103.         if (*this == static_cast<CityTime>(nullptr))
  104.         {
  105.             throw "Null exception";
  106.         }
  107.         else
  108.         {
  109.             FixCityName(city);
  110.             cout << "В городе " << city << " ";
  111.             MyTime::Show();
  112.         }
  113.     }
  114.  
  115.     string FixCityName (string &City)
  116.     {
  117.         City[0] = toupper(City[0]);
  118.         return City;
  119.     }
  120.  
  121.  
  122.     CityTime Sum(CityTime t2)
  123.     {
  124.         return CityTime(hours + t2.hours, minutes + t2.minutes, city + t2.city);
  125.     }
  126.  
  127.     CityTime operator + (const CityTime & t2)
  128.     {
  129.         cout << "quqarequ";
  130.         return CityTime(hours + t2.hours, minutes + t2.minutes, city + t2.city);
  131.     }
  132.    
  133.     ~CityTime()
  134.     {
  135.         cout << "Удаляется объект класса-наследника. \n";
  136.     };
  137. };
  138.  
  139. ostream & operator << (ostream & os, const MyTime & t) // why & ?
  140. {
  141.     os << "\nIt's " << t.hours << " hours and " << t.minutes << " minutes." << endl;
  142.     return os;
  143. }
  144.  
  145. int main()
  146. {
  147.     setlocale(LC_ALL, "rus");
  148.     srand(time(NULL));
  149.  
  150. //  CityTime MoscowTime = CityTime(18, 22, "москва");
  151. //  MoscowTime.Show();
  152.  
  153.     string cityArr[] = { "москва", "Лондон", "шанхай", "Вашингтон", "Дублин" };
  154.     MyTime** timeArr = new MyTime* [10];
  155.     for (int i = 0; i < 10; i++)
  156.     {
  157.         if (i % 2 == 0)
  158.         {
  159.             timeArr[i] = new MyTime(rand(), rand());
  160.             timeArr[i]->Show();
  161.         }
  162.         else
  163.         {
  164.             timeArr[i] = new CityTime(rand(), rand(), cityArr[i / 2]);
  165.             timeArr[i]->Show();
  166.         }
  167.     }
  168.  
  169.     CityTime *c = static_cast<CityTime*>(timeArr[1]);
  170.     CityTime *d = static_cast<CityTime*>(timeArr[3]);
  171.  
  172.     (*c+*d).Show();
  173.     (c->Sum(*d)).Show();
  174.  
  175.     delete [] (c);
  176. /*  try {
  177.         (*c).Show();
  178.     }
  179.     catch (string city) {
  180.         cout << "Объект удален";
  181.     }
  182.     catch (int hours) {
  183.         cout << "Объект удален";
  184.     }
  185.     catch (int minutes) {
  186.         cout << "Объект удален";
  187.     }
  188.    
  189.     for (int i = 0; i < 10; i++) {
  190.         delete timeArr[i];
  191.     } */
  192.     (*c).Show();
  193.  
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement