Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class time {
- private:
- int hour, minute, sec, millisec;
- public:
- time() {
- cout << "Вызвался конструктор по умолчанию\n";
- cout << this << "\n";
- hour = 0;
- minute = 0;
- sec = 0;
- millisec = 0;
- }
- time(int h, int m, int s, int ms) {
- cout << "Вызвался конструктор с параматром\n";
- cout << this << "\n";
- hour = h;
- minute = m;
- sec = s;
- millisec = ms;
- }
- ~time() {
- cout << "Вызвался деструктор " << this << "\n";
- }
- void SetHour(int h);
- void SetMinute(int m);
- void SetSec(int s);
- void SetMillisec(int ms);
- void Print();
- int GetHour() const;
- int GetMinute() const;
- int GetSec() const;
- int GetMillisec() const;
- time operator -(time & other) {
- cout << "Вызвался оператор -\n" << this << "\n";
- time temp;
- int sM1 = 0, sM2 = 0,sM3=0;
- sM1 = this->hour * 3600000 + this->minute * 60000 + this->sec * 1000 + this->millisec;
- sM2 = other.hour * 3600000 + other.minute * 60000 + other.sec * 1000 + other.millisec;
- sM3 = abs(sM1 - sM2);
- temp.millisec = sM3 % 1000;
- sM3 /= 1000;
- temp.hour = sM3 / 3600;
- while (temp.hour > 23) temp.hour -= 24;
- sM3 %= 3600;
- temp.minute = sM3 / 60;
- while (temp.minute > 59) temp.minute -= 60;
- sM3 %= 60;
- temp.sec = sM3;
- while (temp.sec > 59) temp.sec -= 60;
- return temp;
- }
- time (const time & other) {
- cout << "Вызвался конструктор копирования\n";
- cout << this << "\n";
- this->hour = other.hour;
- this->minute = other.minute;
- this->sec = other.sec;
- this->millisec = other.millisec;
- }
- time operator +(time & other) {
- cout << "Вызвался оператор +\n" << this << "\n";
- time temp;
- int sM1 = 0, sM2 = 0, sM3 = 0;
- sM1 = this->hour * 3600000 + this->minute * 60000 + this->sec * 1000 + this->millisec;
- sM2 = other.hour * 3600000 + other.minute * 60000 + other.sec * 1000 + other.millisec;
- sM3 = sM1 + sM2;
- temp.millisec = sM3 % 1000;
- sM3 /= 1000;
- temp.hour = sM3 / 3600;
- while (temp.hour > 23) temp.hour -= 24;
- sM3 %= 3600;
- temp.minute = sM3 / 60;
- while (temp.minute > 59) temp.minute -= 60;
- sM3 %= 60;
- temp.sec = sM3;
- while (temp.sec > 59) temp.sec -= 60;
- return temp;
- }
- time & operator = (const time & other) {
- this->hour = other.hour;
- this->minute = other.minute;
- this->sec = other.sec;
- this->millisec = other.millisec;
- }
- };
- void time::SetHour(int h) {
- hour = h;
- }
- void time::SetMinute(int m) {
- minute = m;
- }
- void time::SetSec(int s) {
- sec = s;
- }
- void time::SetMillisec(int ms) {
- millisec = ms;
- }
- int time::GetHour() const {
- return hour;
- }
- int time::GetMinute() const {
- return minute;
- }
- int time::GetSec() const{
- return sec;
- }
- int time::GetMillisec() const {
- return millisec;
- }
- void time::Print() {
- cout << "Время: " << GetHour() << ":" << GetMinute() << ":" << GetSec() << ":" << GetMillisec() << "\n\n";
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int h, m, s, ms;
- cin >> h >> m >> s >> ms;
- time to(h,m,s,ms);
- to.Print();
- time to2(12, 0, 34, 0);
- to2.Print();
- time to3 = to + to2;
- to3.Print();
- time to4 = to - to2;
- to4.Print();
- time to5 = to;
- to5.Print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement