Solved: 20 - Classes and Objects
This commit is contained in:
@@ -11,8 +11,8 @@ public:
|
||||
[[nodiscard]] auto owner() const -> const std::string&;
|
||||
[[nodiscard]] auto balance() const -> double;
|
||||
|
||||
void deposit(double amount);
|
||||
auto withdraw(double amount) -> bool;
|
||||
void deposit(double amount) pre(amount > 0);
|
||||
auto withdraw(double amount) -> bool pre(amount > 0);
|
||||
|
||||
private:
|
||||
std::string owner_;
|
||||
|
||||
@@ -2,20 +2,23 @@
|
||||
|
||||
namespace oop {
|
||||
|
||||
BankAccount::BankAccount(std::string owner, double balance)
|
||||
BankAccount::BankAccount(std::string owner, const double balance)
|
||||
: owner_(std::move(owner)), balance_(balance) {}
|
||||
|
||||
auto BankAccount::owner() const -> const std::string& { return owner_; }
|
||||
auto BankAccount::balance() const -> double { return balance_; }
|
||||
|
||||
void BankAccount::deposit(double amount) {
|
||||
// TODO: Add amount when positive
|
||||
(void)amount;
|
||||
void BankAccount::deposit(const double amount)
|
||||
{
|
||||
this->balance_ += amount;
|
||||
}
|
||||
|
||||
auto BankAccount::withdraw(double amount) -> bool {
|
||||
// TODO: Return false if insufficient funds, else subtract and return true
|
||||
(void)amount;
|
||||
auto BankAccount::withdraw(const double amount) -> bool
|
||||
{
|
||||
if (this->balance_ >= amount) {
|
||||
this->balance_ -= amount;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ TEST(ClassesAndObjects, DepositAndWithdraw) {
|
||||
EXPECT_TRUE(account.withdraw(30.0));
|
||||
EXPECT_DOUBLE_EQ(account.balance(), 120.0);
|
||||
EXPECT_FALSE(account.withdraw(1000.0));
|
||||
EXPECT_DEATH(account.withdraw(-10.0), "amount > 0");
|
||||
EXPECT_DEATH(account.deposit(-10.0), "amount > 0");
|
||||
}
|
||||
|
||||
TEST(ClassesAndObjects, OwnerName) {
|
||||
|
||||
Reference in New Issue
Block a user