forked from natto1784/ooplab
56
lab2/5.cpp
Normal file
56
lab2/5.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Time {
|
||||
private:
|
||||
uint h, m, s;
|
||||
|
||||
public:
|
||||
Time() = default;
|
||||
Time(uint, uint, uint);
|
||||
Time operator+(Time);
|
||||
void display();
|
||||
};
|
||||
|
||||
Time::Time(uint h, uint m, uint s) {
|
||||
if (m > 59 || s > 59)
|
||||
exit(1);
|
||||
|
||||
this->h = h;
|
||||
this->m = m;
|
||||
this->s = s;
|
||||
}
|
||||
|
||||
void Time::display() {
|
||||
cout << "Hours: " << this->h << endl
|
||||
<< "Minutes: " << this->m << endl
|
||||
<< "Seconds: " << this->s << endl;
|
||||
}
|
||||
|
||||
Time Time::operator+(Time op) {
|
||||
Time t;
|
||||
|
||||
t.h = this->h + op.h;
|
||||
t.m = this->m + op.m;
|
||||
t.s = this->s + op.s;
|
||||
|
||||
t.m += t.s / 60;
|
||||
t.s %= 60;
|
||||
|
||||
t.h += t.m / 60;
|
||||
t.m %= 60;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Time t;
|
||||
Time a(64, 32, 7);
|
||||
Time b(3, 59, 53);
|
||||
|
||||
t = a + b;
|
||||
|
||||
t.display();
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user