// A bank account class #include "stdafx.h" #include #include class Account { // private members are those that cannot be changed directly by the owner // of the account. The bank has access to them and may change them. private: double balance; std::string name; // the interest rate both constant, and static. This means the value // is the same (const) for all instances of the class, so we only need // one version (static) static const double interestRate; public: // public access // here the public or account user can change the // balance only by depositing cash (using deposit function) // or by withdrawing cash (using withdraw) void withdraw(double amount) // withdraw cash { balance = balance - amount;} void deposit(double amount) // deposit cash { balance = balance + amount;} void endYear() // pay interest at the end of the Year { balance = (1+interestRate)*balance; } void printBalance() { std::cout << name <<"'s balance is " << balance << " \n"; } // constructor function, can't use defaults now // as we need to specify a name when we open the account Account(int initial_balance,std::string name_) { balance = initial_balance; name=name_;} }; // static members must be initialised outside the class const double Account::interestRate=0.05; int main() { // create an account, must specify initial balance and name Account currentAccount(0.,"Paul Johnson"); for(int i=1;i<=10;i++) { // deposit some money currentAccount.deposit(100); // pay interest currentAccount.endYear(); } // display balance currentAccount.printBalance(); }