online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include "account.h" #include "bank.h" #include "interest_calculator.h" #include "logger.h" void CreateAccounts(Bank* bank, const IInterestCalculator& interest_calculator) { for (auto i = 0; i < 10; i++) { auto account = bank->CreateAccount(interest_calculator); account->Deposit(i * 2.0); } } int main() { Logger::LogLine("Banking program: Start"); Bank bank; InterestCalculator checking_account_interest_calculator(0.001); CreateAccounts(&bank, checking_account_interest_calculator); InterestCalculator savings_account_interest_calculator(0.0075); CreateAccounts(&bank, savings_account_interest_calculator); bank.PayInterest(); Logger::LogLine("\nBanking program: End"); return 0; }
#ifndef ACCOUNT_H_ #define ACCOUNT_H_ #include "i_interest_calculator.h" class Account { private: double balance_; const IInterestCalculator& interest_calculator_; public: explicit Account(const IInterestCalculator& interest_calculator) noexcept; double GetBalance() const noexcept; void Deposit(double amount); double Withdraw(double amount); double CalculateInterest() const; }; #endif // ACCOUNT_H_
#include "account.h" #include "logger.h" Account::Account(const IInterestCalculator& interest_calculator) noexcept : balance_{0.0}, interest_calculator_{interest_calculator} { } double Account::GetBalance() const noexcept { return balance_; } void Account::Deposit(const double amount) { balance_ += amount; Logger::Log("\nDeposit, balance: "); Logger::LogLine(balance_); } double Account::Withdraw(const double amount) { balance_ -= amount; Logger::Log("\nWithdraw, balance: "); Logger::LogLine(balance_); return amount; } double Account::CalculateInterest() const { const auto interest = interest_calculator_.CalculateInterest(balance_); Logger::Log("\nCalculate interest, amount: "); Logger::LogLine(interest); return interest; }
#ifndef BANK_H_ #define BANK_H_ #include <list> #include <memory> #include "i_interest_calculator.h" class Account; class Bank { private: std::list<std::shared_ptr<Account>> accounts_; public: std::shared_ptr<Account> CreateAccount(const IInterestCalculator& interest_calculator); void PayInterest(); }; #endif // BANK_H_
#include "bank.h" #include <ctime> #include "account.h" #include "logger.h" std::shared_ptr<Account> Bank::CreateAccount( const IInterestCalculator& interest_calculator) { auto account = std::make_shared<Account>(interest_calculator); accounts_.push_back(account); Logger::Log("\nCreate account, number of accounts: "); Logger::LogLine(accounts_.size()); return account; } void Bank::PayInterest() { for (auto& account : accounts_) { const auto interest = account->CalculateInterest(); account->Deposit(interest); } Logger::Log("\nInterest paid on: "); time_t current_time; time(&current_time); auto time_string = ctime(&current_time); if (time_string != nullptr) { Logger::LogLine(time_string); } }
#ifndef I_INTEREST_CALCULATOR_H_ #define I_INTEREST_CALCULATOR_H_ class IInterestCalculator { public: virtual ~IInterestCalculator() = default; virtual double CalculateInterest(double balance) const = 0; }; #endif // I_INTEREST_CALCULATOR_H_
#ifndef INTEREST_CALCULATOR_H_ #define INTEREST_CALCULATOR_H_ #include "i_interest_calculator.h" class InterestCalculator : public IInterestCalculator { private: double interest_rate_{}; public: explicit InterestCalculator(double interest_rate); double GetInterestRate() const; double CalculateInterest(double balance) const override; }; #endif // INTEREST_CALCULATOR_H_
#include "interest_calculator.h" InterestCalculator::InterestCalculator(const double interest_rate) : interest_rate_{interest_rate} { } double InterestCalculator::GetInterestRate() const { return interest_rate_; } double InterestCalculator::CalculateInterest(const double balance) const { return balance * interest_rate_; }
#ifndef LOGGER_H_ #define LOGGER_H_ #include <iostream> class Logger { public: template <class T> static void Log(const T& message) { LogIntern(message, ""); } template <class T> static void LogLine(const T& message) { LogIntern(message, "\n"); } template <class T> static void LogIntern(const T& message, const char* additional_text) { std::cout << message << additional_text; } }; #endif // LOGGER_H_

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