#include "Clock.h"
extern void appl(IClock&);
int main()
{
std::cout.setf(std::ios::boolalpha);
Clock c;
appl(c);
}
#ifndef ICLOCK_H
#define ICLOCK_H
#include <iosfwd>
class IClock {
public:
virtual ~IClock() {}
virtual void Set(int, int, int) =0;
virtual void TickUp(int = 1) =0;
virtual std::ostream& Print(std::ostream&) const =0;
};
inline
std::ostream& operator<<(std::ostream& lhs, const IClock& rhs) {
return rhs.Print(lhs);
}
#endif
#ifndef CLOCK_H
#define CLOCK_H
#include <iosfwd>
#include <cstring>
#include <iostream>
#include "IClock.h"
#include "Counter.h"
class Clock : public IClock {
Counter hours_;
Counter minutes_;
Counter seconds_;
public:
Clock(int hours = 0, int minutes = 0, int seconds = 0)
: hours_(24)
, minutes_(60, &hours_)
, seconds_(60, &minutes_)
{
Set(hours, minutes, seconds);
}
Clock(const Clock&); // will NOT be implemented
Clock& operator=(const Clock&); // will NOT be implemented
virtual void Set(int hours, int minutes, int seconds) {
hours_.SetValue(hours);
minutes_.SetValue(minutes);
seconds_.SetValue(seconds);
}
virtual void TickUp(int seconds)
{ seconds_.Count(seconds); }
virtual std::ostream& Print(std::ostream&) const;
};
#endif // CLOCK_H
#include "Clock.h"
#include <iostream>
#include <iomanip>
std::ostream& Clock::Print(std::ostream& s) const {
std::ostream os(s.rdbuf());
os.fill('0');
os << std::setw(2) << hours_.GetValue() << ':'
<< std::setw(2) << minutes_.GetValue() << ':'
<< std::setw(2) << seconds_.GetValue();
return s;
}
#ifndef COUNTER_H
#define COUNTER_H
#include <cstddef> // for NULL
#include <limits>
class Counter {
private:
int value_;
const int max_value_;
Counter* next_counter_;
public:
Counter()
: value_(0)
, max_value_(std::numeric_limits<int>::max())
, next_counter_(NULL)
{}
Counter(int max_value, Counter* next_counter = NULL)
: value_(0)
, max_value_(max_value)
, next_counter_(next_counter)
{}
Counter(Counter* next_counter = NULL)
: value_(0)
, max_value_(std::numeric_limits<int>::max())
, next_counter_(next_counter)
{}
Counter(const Counter&); // will NOt be implemented
Counter& operator=(const Counter&); // will NOT be implemented
void SetValue(int value)
{ value_ = value; }
int GetValue() const
{ return value_; }
void Count(int amount = 1);
void Reset();
};
#endif // COUNTER_H
#include "Counter.h"
void Counter::Count(int amount) {
while (amount-- > 0) {
if (++value_ == max_value_) {
value_ = 0;
if (next_counter_) {
next_counter_->Count(1);
}
}
}
}
void Counter::Reset() {
value_ = 0;
}
#include <iostream>
#define SHOW(...)\
(void)(std::cout << __FILE__ << ':' << __LINE__ << '\t'\
<< #__VA_ARGS__ << " --> " << (__VA_ARGS__)\
<< std::endl)
#include "IClock.h"
void appl(IClock& c) {
c.TickUp(); SHOW(c);
c.TickUp(); SHOW(c);
c.TickUp(); SHOW(c);
c.Set(0, 0, 58); SHOW(c);
c.TickUp(); SHOW(c);
c.TickUp(); SHOW(c);
c.TickUp(); SHOW(c);
c.TickUp(60); SHOW(c);
c.TickUp(600); SHOW(c);
}
# Adding an Interface to the Clock
Version of `clock-02` using C++98 features only.
all: main run
run: main
./main
main: *.cpp *.h
g++ -std=c++98 *.cpp -o $@
clean:
-rm -f *.o a.out core main
zip: clean
zip ../clock-02a.zip *.txt *.cpp *.h Makefile
.PHONY: all run clean zip