online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include "Clock.h" extern void appl(IClock&); int main() { std::cout.setf(std::ios::boolalpha); Clock c{}; appl(c); }
#include <iostream> #define SHOW(...)\ (void)(std::cout << __FILE__ << ':' << __LINE__ << '\t'\ << #__VA_ARGS__ << " --> " << (__VA_ARGS__)\ << std::endl) #include "IClock.h" void appl(IClock& c) { SHOW(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.Set(0, 59, 58); SHOW(c); c.TickUp(); SHOW(c); c.TickUp(); SHOW(c); c.TickUp(60); SHOW(c); c.TickUp(600); SHOW(c); }
#ifndef ICLOCK_H #define ICLOCK_H #include <iosfwd> class IClock { public: virtual ~IClock() =default; virtual void Set(int, int, int) =0; virtual void TickUp(int = 1) =0; virtual void Print(std::ostream&) const =0; }; inline std::ostream& operator<<(std::ostream& lhs, const IClock& rhs) { rhs.Print(lhs); return lhs; } #endif
#ifndef CLOCK_H #define CLOCK_H #include <iosfwd> #include <cstring> #include <iostream> #include "IClock.h" #include "LimitCounter.h" #include "OverflowCounter.h" class Clock : public IClock { LimitCounter hours_; OverflowCounter minutes_; OverflowCounter 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&) =delete; // copy c'tor Clock& operator=(const Clock&) =delete; // copy assignment Clock(Clock&&) =delete; // move c'tor Clock& operator=(Clock&&) =delete; // move assignment virtual void Set(int hours, int minutes, int seconds) override { hours_.SetValue(hours); minutes_.SetValue(minutes); seconds_.SetValue(seconds); } virtual void TickUp(int seconds) override { seconds_.Count(seconds); } virtual void Print(std::ostream&) const override; }; #endif
#include "Clock.h" #include <iostream> #include <iomanip> void 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(); }
#ifndef ICOUNTER_H #define ICOUNTER_H class ICounter { public: virtual ~ICounter() =default; virtual void Count() =0; virtual void Reset() =0; }; #endif
#ifndef COUNTER_H #define COUNTER_H #include "ICounter.h" class LimitCounter : public ICounter { private: int value_{}; const int max_value_{}; virtual void WillOverflow() {} virtual void WillReset() {} virtual void HasOverflowed() {} virtual void HasResetted() {} public: LimitCounter() =delete; LimitCounter(int max_value) : value_{0} , max_value_{max_value} {} void SetValue(int value) { value_ = value; } int GetValue() const { return value_; } void Count(int amount); virtual void Count() override final; virtual void Reset() override final; }; #endif
#include "LimitCounter.h" void LimitCounter::Count(int amount) { while (amount-- > 0) { Count(); } } void LimitCounter::Count() { if (++value_ >= max_value_) { WillOverflow(); value_ = 0; HasOverflowed(); } } void LimitCounter::Reset() { WillReset(); value_ = 0; HasResetted(); }
#ifndef OVERFLOW_COUNTER_H #define OVERFLOW_COUNTER_H #include "LimitCounter.h" class OverflowCounter : public LimitCounter { ICounter& next_counter_; public: OverflowCounter(int max_value, ICounter& next_counter) : LimitCounter{max_value} , next_counter_{next_counter} {} virtual void HasOverflowed() override; virtual void HasResetted() override; }; #endif
#include "OverflowCounter.h" void OverflowCounter::HasOverflowed() { next_counter_.Count(); } void OverflowCounter::HasResetted() { next_counter_.Reset(); }
clock-5: g++ -std=c++14 *.cpp -o $@ run: clock-5 ./clock-5 clean: -rm -f *.o a.out core clock-5 zip: zip ../clock-5.zip *.txt *.cpp *.h
Adding an Interface `ICounter` ------------------------------ The inheritance here is: +-----------------+ | <<interface>> | | ICounter | | | +--------.--------+ /_\ | | +-----------------+ | <<class>> | | LimitCounter | | | +--------.--------+ /_\ | | +-----------------+ | <<class>> | | OverflowCounter | | | +-----------------+ The interface `ICounter` is implemented by `LimitCounter`, which subsequently defines the extension points and is THE BASE CLASS of `OverflowCounter` (= Inheritance) which fills the extension points to implement the added behavior. As derived class `OverflowCounter` ALSO INHERITS the interface implementations from `LimitCounter`. But as the public members there are `final` (as the following the NVI-idiom recommends.) From the viewpoint of the LSP `OverflowCounter` also IS A `LimitCounter`.
To advance to the next step apply the following changes: * With the interface in place the design is now ready to * replace "extension points" provided via virtual member functions implemented in derived classes * with member functions implemented in a base class provided as template argument. NOTE: Until templates are covered in more depth this is definitely advanced stuff. So maybe give the following outline just a few thoughts but look up into the solutions and wait for the explanations given. * In case you want to go on your own, here are the principle modifications to `LimitCounter`: template<typename Details> class Counter_Base : public ICounter, private Details { using Details::WillOverflow; using Details::HasResetted; public: ... // more or less as before }; class LimitCounter_Details { protected: void WillOverflow() {} //empty implementation void HasResetted() {} //empty implementation }; using LimitCounter = Counter_Base<LimitCounter_Details>; * And here is `OverflowCounter`: class OverflowCounter_Details { ICounter& next_counter_; protected: void WillOverflow() { next_counter_.Count(); } void HasResetted() { next_counter_.Reset(); } }; using OverflowCounter = Counter_Base<OverflowCounter_Details>; ================================================================= Optional/Advanced: * Actually implement the above, which partially is just "copy pasting" the above into the appropriate files (header files only, as usual with templates. * The challenge (still) to solve is how `next_counter_` gets initialized, as these now comes from a derived class (ie. `Counter_Base` must somehow prepare for it).

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue