feat: implement CPP06 C++20 card
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
#include <iostream>
|
||||
class Temperature { public: explicit Temperature(int value) : value_{value} {} bool set(int value) { if (value < -50 || value > 150) return false; value_ = value; return true; } int value() const { return value_; } private: int value_; };
|
||||
int main() { Temperature temperature{20}; const bool accepted = temperature.set(25); const bool rejected = !temperature.set(400); std::cout << "accepted=" << accepted << " rejected=" << rejected << " value=" << temperature.value() << '\n'; }
|
||||
@@ -0,0 +1,3 @@
|
||||
#include <iostream>
|
||||
class QueueDepth { public: explicit QueueDepth(int capacity) : capacity_{capacity} {} bool push() { if (depth_ == capacity_) return false; ++depth_; return true; } bool pop() { if (depth_ == 0) return false; --depth_; return true; } int depth() const { return depth_; } private: int capacity_; int depth_{}; };
|
||||
int main() { QueueDepth queue{2}; int pushes{}; pushes += queue.push(); pushes += queue.push(); const bool overflow = !queue.push(); int pops{}; pops += queue.pop(); pops += queue.pop(); const bool empty = !queue.pop(); std::cout << "pushes=" << pushes << " overflow=" << overflow << " pops=" << pops << " empty=" << empty << " depth=" << queue.depth() << '\n'; }
|
||||
@@ -0,0 +1,3 @@
|
||||
#include <iostream>
|
||||
class Interval { public: Interval(int first, int second) : start_{first < second ? first : second}, end_{first < second ? second : first} {} int start() const { return start_; } int end() const { return end_; } int width() const { return end_ - start_; } private: int start_; int end_; };
|
||||
int main() { Interval interval{10, 3}; std::cout << "start=" << interval.start() << " end=" << interval.end() << " width=" << interval.width() << '\n'; }
|
||||
Reference in New Issue
Block a user