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;
- static int count;
- mutable int mutID;
- public:
- time();
- time(int h, int m, int s, int ms);
- ~time();
- void SetHour(int h);
- void SetMinute(int m);
- void SetSec(int s);
- void SetMillisec(int ms);
- void SetMutID(int x) const;
- int GetHour() const;
- int GetMinute() const;
- int GetSec() const;
- int GetMillisec() const;
- static int GetCount();
- int GetMutID() const;
- void Print() const;
- time(const time& other);
- time operator -(time& other);
- time operator +(time& other);
- time& operator = (const time& other) {
- cout << "Вызвался оператор =\n" << this << "\n";
- this->hour = other.hour;
- this->minute = other.minute;
- this->sec = other.sec;
- this->millisec = other.millisec;
- return *this;
- }
- };
- int time::count = 0;
- time::time() {
- cout << "Вызвался конструктор по умолчанию\n";
- cout << this << "\n\n";
- hour = 0;
- minute = 0;
- sec = 0;
- millisec = 0;
- count++;
- mutID =count;
- }
- time::time(int h, int m, int s, int ms) {
- cout << "Вызвался конструктор с параматром\n";
- cout << this << "\n\n";
- hour = h;
- minute = m;
- sec = s;
- millisec = ms;
- count++;
- mutID = count;
- }
- time::~time() {
- cout << "Вызвался деструктор " << this << "\n";
- count--;
- }
- time::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;
- count++;
- mutID = count;
- }
- time time:: operator -(time& other) {
- cout << "Вызвался оператор -\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 time:: operator +(time& other) {
- cout << "Вызвался оператор +\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;
- }
- 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;
- }
- void time::SetMutID(int x)const{
- mutID = x;
- }
- int time::GetHour() const {
- return hour;
- }
- int time::GetMinute() const {
- return minute;
- }
- int time::GetSec() const {
- return sec;
- }
- int time::GetMillisec() const {
- return millisec;
- }
- int time::GetCount() {
- return count;
- }
- int time::GetMutID() const {
- return mutID;
- }
- void time::Print() const {
- cout << "Время: " << GetHour() << ":" << GetMinute() << ":" << GetSec() << ":" << GetMillisec() << "\n\n";
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int h, m, s, ms;
- cout << "Введите часы, минуты, секунды и миллисекунды 1 оъекта\n";
- cin >> h >> m >> s >> ms;
- time to1;
- to1.SetHour(h);
- to1.SetMinute(m);
- to1.SetSec(s);
- to1.SetMillisec(ms);
- cout << "Введите часы, минуты, секунды и миллисекунды 2 оъекта\n";
- cin >> h >> m >> s >> ms;
- time to2(h, m, s, ms);
- time to3, to4, to5;
- to3 = to1 + to2;
- to4 = to1 - to2;
- to5 = to1;
- const time constto(15, 20, 25, 300);
- constto.SetMutID(15);
- cout<<constto.GetMutID()<<"\n";
- to1.Print();
- to2.Print();
- to3.Print();
- to4.Print();
- to5.Print();
- constto.Print();
- cout << to1.GetMutID() << "\n";
- cout << to2.GetMutID() << "\n";
- cout << to3.GetMutID() << "\n";
- cout << to4.GetMutID() << "\n";
- cout << to5.GetMutID() << "\n";
- cout << constto.GetMutID() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement