online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include "bank.h" #include "checkingaccount.h" #include "i_account.h" #include "savingsaccount.h" int main() { Bank bank; CheckingAccount::SetInterestRate(0.001); SavingsAccount::SetInterestRate(0.0075); for (auto i = 0; i < 50; i++) { auto account = bank.OpenCheckingAccount(); account->Deposit(i * 100.0); } for (auto i = 0; i < 50; i++) { auto account = bank.OpenSavingsAccount(); account->Deposit(i * 100.0); } // ToDo: Create 20 pocket money accounts (5.00 EUR initial balance) bank.PayInterest(); return 0; }
#ifndef BANK_H_ #define BANK_H_ #include <list> #include <memory> class IAccount; class Bank { private: std::list<std::shared_ptr<IAccount>> accounts_; public: std::shared_ptr<IAccount> OpenSavingsAccount(); std::shared_ptr<IAccount> OpenCheckingAccount(); // ToDo: Create a pocket money account (class PocketMoneyAccount) void PayInterest(); private: std::shared_ptr<IAccount> OpenAccount(std::shared_ptr<IAccount> new_account); }; #endif // BANK_H_
#include "bank.h" #include "checkingaccount.h" #include "savingsaccount.h" std::shared_ptr<IAccount> Bank::OpenSavingsAccount() { const auto account = std::make_shared<SavingsAccount>(); return OpenAccount(account); } std::shared_ptr<IAccount> Bank::OpenCheckingAccount() { const auto account = std::make_shared<CheckingAccount>(); return OpenAccount(account); } // ToDo: Create a pocket money account (class PocketMoneyAccount) std::shared_ptr<IAccount> Bank::OpenAccount( std::shared_ptr<IAccount> new_account) { accounts_.push_back(new_account); return new_account; } void Bank::PayInterest() { for (auto& account : accounts_) { const auto interest = account->CalculateInterest(); account->Deposit(interest); } }
#ifndef CHECKINGACCOUNT_H_ #define CHECKINGACCOUNT_H_ #include "i_account.h" class CheckingAccount : public IAccount { private: static double interest_rate_; double balance_{}; double overdraft_{}; public: static double GetInterestRate() noexcept; static void SetInterestRate(double interest_rate) noexcept; double GetBalance() const noexcept; void Deposit(double amount) noexcept override; double Withdraw(double amount) noexcept override; double CalculateInterest() noexcept override; void SetOverdraft(double amount) noexcept override; }; #endif // CHECKINGACCOUNT_H_
#include "checkingaccount.h" double CheckingAccount::interest_rate_{}; double CheckingAccount::GetInterestRate() noexcept { return interest_rate_; } void CheckingAccount::SetInterestRate(const double interest_rate) noexcept { interest_rate_ = interest_rate; } double CheckingAccount::GetBalance() const noexcept { return balance_; } void CheckingAccount::Deposit(const double amount) noexcept { balance_ += amount; } double CheckingAccount::Withdraw(const double amount) noexcept { double withdrawn; if (balance_ + overdraft_ > amount) { withdrawn = amount; } else if (balance_ + overdraft_ > 0.0) { withdrawn = balance_ + overdraft_; } else { withdrawn = 0.0; } balance_ -= withdrawn; return withdrawn; } double CheckingAccount::CalculateInterest() noexcept { const auto interest = GetBalance() * GetInterestRate(); return interest; } void CheckingAccount::SetOverdraft(const double amount) noexcept { overdraft_ = amount; }
#ifndef I_ACCOUNT_H_ #define I_ACCOUNT_H_ class IAccount { public: virtual ~IAccount() = default; virtual void Deposit(double amount) = 0; virtual double Withdraw(double amount) = 0; virtual double CalculateInterest() = 0; virtual void SetOverdraft(double amount) = 0; }; #endif // I_ACCOUNT_H_
#ifndef SAVINGSACCOUNT_H_ #define SAVINGSACCOUNT_H_ #include "i_account.h" class SavingsAccount : public IAccount { private: static double interest_rate_; double balance_{}; public: static double GetInterestRate() noexcept; static void SetInterestRate(double interest_rate) noexcept; double GetBalance() const noexcept; void Deposit(double amount) noexcept override; double Withdraw(double amount) noexcept override; double CalculateInterest() noexcept override; void SetOverdraft(double amount) noexcept override; }; #endif // SAVINGSACCOUNT_H_
#include "savingsaccount.h" double SavingsAccount::interest_rate_{}; double SavingsAccount::GetInterestRate() noexcept { return interest_rate_; } void SavingsAccount::SetInterestRate(const double interest_rate) noexcept { interest_rate_ = interest_rate; } double SavingsAccount::GetBalance() const noexcept { return balance_; } void SavingsAccount::Deposit(const double amount) noexcept { balance_ += amount; } double SavingsAccount::Withdraw(const double amount) noexcept { balance_ -= amount; return amount; } double SavingsAccount::CalculateInterest() noexcept { const auto interest = GetBalance() * GetInterestRate(); return interest; } void SavingsAccount::SetOverdraft(double amount) noexcept {}

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