#include "Clock.h"
#include <iostream>
int main()
{
Clock c{}; std::cout << c << std::endl;
c.TickUp(); std::cout << c << std::endl;
c.TickUp(); std::cout << c << std::endl;
c.TickUp(); std::cout << c << std::endl;
c.Set(0, 0, 58);
c.TickUp(); std::cout << c << std::endl;
c.TickUp(); std::cout << c << std::endl;
c.TickUp(); std::cout << c << std::endl;
c.TickUp(60); std::cout << c << std::endl;
c.TickUp(60); std::cout << c << std::endl;
Clock c2{23, 57, 58}; std::cout << c2<< std::endl;
c2.TickUp(60); std::cout << c2 << std::endl;
c2.TickUp(60); std::cout << c2 << std::endl;
c2.TickUp(); std::cout << c2 << std::endl;
c2.TickUp(); std::cout << c2 << std::endl;
c2.TickUp(); std::cout << c2 << std::endl;
}
#ifndef CLOCK_H
#define CLOCK_H
#include <iosfwd>
class Clock {
int hours_{0};
int minutes_{0};
int seconds_{0};
public:
Clock() =default;
Clock(int hours, int minutes, int seconds)
: hours_{hours}
, minutes_{minutes}
, seconds_{seconds}
{}
Clock(const Clock&) =delete;
Clock& operator=(const Clock&) =delete;
void Set(int hours, int minutes, int seconds) {
hours_ = hours;
minutes_ = minutes;
seconds_ = seconds;
}
void TickUp(int seconds = 1);
friend std::ostream& operator<<(std::ostream&, const Clock&);
};
#endif
#include "Clock.h"
void Clock::TickUp(int n) {
while (n-- > 0) {
if (++seconds_ >= 60) {
seconds_ = 0;
if (++minutes_ >= 60) {
minutes_ = 0;
if (++hours_ >= 24) {
hours_ = 0;
}
}
}
}
}
#include <iostream>
#include <iomanip>
std::ostream& operator<<(std::ostream& lhs, const Clock& rhs) {
std::ostream os{lhs.rdbuf()};
os.fill('0');
os << std::setw(2) << rhs.hours_ << ':'
<< std::setw(2) << rhs.minutes_ << ':'
<< std::setw(2) << rhs.seconds_;
return lhs;
}
#ifndef COUNTER_H
#define COUNTER_H
class Counter {
private:
int value_{};
const int max_value_;
const Display* display_;
Counter* next_counter_;
public:
Counter(int max_value, Counter* next_counter = nullptr);
void SetValue(int value);
int GetValue() const;
void Count(int amount = 1);
void Reset();
}
#endif
Simple example for building a clock
===================================
Hours, minutes, and seconds are held by separate integers.
The overflow is hard-coded within nested conditionals.