Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <math.h>
- #include <string>
- #include <time.h>
- using namespace std;
- class MyTime {
- protected:
- int hours;
- int minutes;
- public:
- MyTime()
- {
- hours = 12;
- minutes = 0;
- }
- MyTime(int H, int M)
- {
- Set(H, M);
- }
- MyTime(int M)
- {
- hours = M / 60 % 24;
- minutes = M % 60;
- FixTime();
- }
- void Set(int H, int M)
- {
- hours = H;
- minutes = M;
- FixTime();
- }
- void FixTime()
- {
- minutes < 60 ? minutes = abs(minutes) : (hours += abs(minutes) / 60) && (minutes = abs(minutes) % 60);
- hours = abs(hours % 24);
- }
- virtual void Show()
- {
- cout << hours << " часов и " << minutes << " минут. \n";
- }
- virtual MyTime Sum(MyTime t2)
- {
- MyTime t;
- t.Set(hours + t2.hours, minutes + t2.minutes);
- return t;
- }
- friend ostream& operator << (ostream & os, const MyTime &);
- virtual MyTime operator + (const MyTime & t2)
- {
- cout << "qq";
- return MyTime(hours + t2.hours, minutes + t2.minutes);
- }
- MyTime operator ++ (int)
- {
- MyTime t = MyTime(this->hours, this->minutes++);
- FixTime();
- return t;
- }
- virtual ~MyTime()
- {
- cout << "Удаляется объект базового класса. \n";
- };
- };
- class CityTime : public MyTime
- {
- private:
- string city;
- public:
- CityTime(int H, int M, string City) : MyTime(H, M), city (City) {}
- void Set(int H, int M, string City)
- {
- MyTime::Set(H, M);
- city = City;
- FixCityName(city);
- }
- void Show()
- {
- if (*this == static_cast<CityTime>(nullptr))
- {
- throw "Null exception";
- }
- else
- {
- FixCityName(city);
- cout << "В городе " << city << " ";
- MyTime::Show();
- }
- }
- string FixCityName (string &City)
- {
- City[0] = toupper(City[0]);
- return City;
- }
- CityTime Sum(CityTime t2)
- {
- return CityTime(hours + t2.hours, minutes + t2.minutes, city + t2.city);
- }
- CityTime operator + (const CityTime & t2)
- {
- cout << "quqarequ";
- return CityTime(hours + t2.hours, minutes + t2.minutes, city + t2.city);
- }
- ~CityTime()
- {
- cout << "Удаляется объект класса-наследника. \n";
- };
- };
- ostream & operator << (ostream & os, const MyTime & t) // why & ?
- {
- os << "\nIt's " << t.hours << " hours and " << t.minutes << " minutes." << endl;
- return os;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- srand(time(NULL));
- // CityTime MoscowTime = CityTime(18, 22, "москва");
- // MoscowTime.Show();
- string cityArr[] = { "москва", "Лондон", "шанхай", "Вашингтон", "Дублин" };
- MyTime** timeArr = new MyTime* [10];
- for (int i = 0; i < 10; i++)
- {
- if (i % 2 == 0)
- {
- timeArr[i] = new MyTime(rand(), rand());
- timeArr[i]->Show();
- }
- else
- {
- timeArr[i] = new CityTime(rand(), rand(), cityArr[i / 2]);
- timeArr[i]->Show();
- }
- }
- CityTime *c = static_cast<CityTime*>(timeArr[1]);
- CityTime *d = static_cast<CityTime*>(timeArr[3]);
- (*c+*d).Show();
- (c->Sum(*d)).Show();
- delete [] (c);
- /* try {
- (*c).Show();
- }
- catch (string city) {
- cout << "Объект удален";
- }
- catch (int hours) {
- cout << "Объект удален";
- }
- catch (int minutes) {
- cout << "Объект удален";
- }
- for (int i = 0; i < 10; i++) {
- delete timeArr[i];
- } */
- (*c).Show();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement