feat: implement CPP05 C++20 card
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
#include "counter.hpp"
|
||||
Counter::Counter(int start) : value_{start} {}
|
||||
void Counter::increment() { ++value_; }
|
||||
int Counter::value() const { return value_; }
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
class Counter { public: explicit Counter(int start); void increment(); int value() const; private: int value_; };
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "counter.hpp"
|
||||
#include <iostream>
|
||||
int main() { Counter counter{2}; counter.increment(); std::cout << "counter=" << counter.value() << '\n'; }
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <iostream>
|
||||
class Account {
|
||||
public:
|
||||
explicit Account(int balance) : balance_{balance} {}
|
||||
bool deposit(int amount) { if (amount <= 0) return false; balance_ += amount; return true; }
|
||||
int balance() const { return balance_; }
|
||||
private: int balance_;
|
||||
};
|
||||
int main() { Account account{10}; const bool accepted = account.deposit(5); const bool rejected = !account.deposit(-2); std::cout << "accepted=" << accepted << " rejected=" << rejected << " balance=" << account.balance() << '\n'; }
|
||||
@@ -0,0 +1,3 @@
|
||||
#include <iostream>
|
||||
class Rectangle { public: Rectangle(int width, int height) : width_{width}, height_{height} {} int area() const { return width_ * height_; } void scale(int factor) { width_ *= factor; height_ *= factor; } private: int width_; int height_; };
|
||||
int main() { Rectangle rectangle{6, 4}; const int before = rectangle.area(); rectangle.scale(2); std::cout << "area=" << before << " scaled=" << rectangle.area() << '\n'; }
|
||||
Reference in New Issue
Block a user