online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include "checkingaccount.h" #include "logger.h" #include "pocketmoneyaccount.h" #include "savingsaccount.h" #include "bank.h" int main() { Logger::Log("Banking program: Start"); 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); } for (auto i = 0; i < 50; i++) { auto account = bank.OpenPocketMoneyAccount(); account->Deposit(5.0); } bank.PayInterest(); Logger::Log("Banking program: End"); return 0; }
#ifndef BANK_H_ #define BANK_H_ #include <list> #include <memory> class Account; class Bank { private: std::list<std::shared_ptr<Account>> accounts_; public: std::shared_ptr<Account> OpenSavingsAccount(); std::shared_ptr<Account> OpenCheckingAccount(); std::shared_ptr<Account> OpenPocketMoneyAccount(); void PayInterest(); private: std::shared_ptr<Account> OpenAccount(std::shared_ptr<Account> new_account); }; #endif // BANK_H_
#include "bank.h" #include <ctime> #include "account.h" #include "checkingaccount.h" #include "logger.h" #include "pocketmoneyaccount.h" #include "savingsaccount.h" std::shared_ptr<Account> Bank::OpenSavingsAccount() { const auto account = std::make_shared<SavingsAccount>(); return OpenAccount(account); } std::shared_ptr<Account> Bank::OpenCheckingAccount() { const auto account = std::make_shared<CheckingAccount>(); return OpenAccount(account); } std::shared_ptr<Account> Bank::OpenPocketMoneyAccount() { const auto account = std::make_shared<PocketMoneyAccount>(); return OpenAccount(account); } std::shared_ptr<Account> Bank::OpenAccount(std::shared_ptr<Account> new_account) { accounts_.push_back(new_account); Logger::Log("Create account, number of accounts: ", accounts_.size()); return new_account; } void Bank::PayInterest() { for (auto& account : accounts_) { const auto interest = account->CalculateInterest(); account->Deposit(interest); } Logger::Log("Interest paid on: "); time_t current_time; time(&current_time); const auto time_string = ctime(&current_time); if (time_string != nullptr) { Logger::Log(&time_string[0]); } }
// this code must not be changed #ifndef ACCOUNT_H_ #define ACCOUNT_H_ class Account { private: static double interest_rate_; double balance_{}; public: virtual ~Account() = default; static double GetInterestRate() noexcept; static void SetInterestRate(double interest_rate) noexcept; double GetBalance() const noexcept; void Deposit(double amount); double Withdraw(double amount); virtual double CalculateInterest(); }; #endif // ACCOUNT_H_
// this code must not be changed #ifndef CHECKINGACCOUNT_H_ #define CHECKINGACCOUNT_H_ #include "account.h" class CheckingAccount : public Account { private: static double interest_rate_; public: static double GetInterestRate() noexcept; static void SetInterestRate(double interest_rate) noexcept; double CalculateInterest() noexcept override; }; #endif // CHECKINGACCOUNT_H_
// this code must not be changed #ifndef LOGGER_H_ #define LOGGER_H_ class Logger { public: static void Log(const char* message); static void Log(const char* message, double value); }; #endif // LOGGER_H_
// this code must not be changed #ifndef POCKETMONEYACCOUNT_H_ #define POCKETMONEYACCOUNT_H_ #include "account.h" class PocketMoneyAccount : public Account { public: double CalculateInterest() override; }; #endif // POCKETMONEYACCOUNT_H_
// this code must not be changed #ifndef SAVINGSACCOUNT_H_ #define SAVINGSACCOUNT_H_ #include "account.h" class SavingsAccount : public Account { private: static double interest_rate_; public: static double GetInterestRate() noexcept; static void SetInterestRate(double interest_rate) noexcept; double CalculateInterest() noexcept override; }; #endif // SAVINGSACCOUNT_H_
// this code must not be changed #include "account.h" #include "logger.h" double Account::interest_rate_{}; double Account::GetInterestRate() noexcept { return interest_rate_; } void Account::SetInterestRate(const double interest_rate) noexcept { interest_rate_ = interest_rate; } double Account::GetBalance() const noexcept { return balance_; } void Account::Deposit(const double amount) { balance_ += amount; Logger::Log("Deposit, balance: ", balance_); } double Account::Withdraw(const double amount) { balance_ -= amount; Logger::Log("Withdraw, balance: ", balance_); return amount; } double Account::CalculateInterest() { return 0.0; }
// this code must not be changed #include "checkingaccount.h" #include "logger.h" double CheckingAccount::interest_rate_{}; double CheckingAccount::GetInterestRate() noexcept { return CheckingAccount::interest_rate_; } void CheckingAccount::SetInterestRate(const double interest_rate) noexcept { CheckingAccount::interest_rate_ = interest_rate; } double CheckingAccount::CalculateInterest() noexcept { const auto interest = GetBalance() * CheckingAccount::GetInterestRate(); Logger::Log("Calculate interest, amount: ", interest); return interest; }
// this code must not be changed #include "logger.h" #include <iostream> void Logger::Log(const char* message) { std::cout << message << "\n"; } void Logger::Log(const char* message, const double value) { std::cout << message << value << "\n"; }
// this code must not be changed #include "pocketmoneyaccount.h" double PocketMoneyAccount::CalculateInterest() { throw "A pocket money account gets no interest!"; }
// this code must not be changed #include "savingsaccount.h" #include "logger.h" double SavingsAccount::interest_rate_{}; double SavingsAccount::GetInterestRate() noexcept { return SavingsAccount::interest_rate_; } void SavingsAccount::SetInterestRate(const double interest_rate) noexcept { SavingsAccount::interest_rate_ = interest_rate; } double SavingsAccount::CalculateInterest() noexcept { const auto interest = GetBalance() * SavingsAccount::GetInterestRate(); Logger::Log("Calculate interest, amount: ", interest); return interest; }

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