// File: account.cpp // ----------------------------------------------- // // account class example // // ----------------------------------------------- #include using namespace std; // ----------------------------------------------- class account { double balance; public: account (void) : balance(0) {} account (double start) : balance(start) {} void deposit (double); double withdraw (double); double getBalance (void); }; // ----------------------------------------------- // // implementation of member functions // (definition) // // ----------------------------------------------- void account::deposit (double amount) { balance += amount; } double account::getBalance (void) { return balance; } double account::withdraw (double amount) { if (balance >= amount) { balance -= amount; return amount; } else { cerr << "You are broke" << endl; amount = balance; balance = 0.0; return amount; } } // ----------------------------------------------- int main (void) { account acc1; account acc2(5000.0); cout << "Balance (default constructor): "; cout << acc1.getBalance () << endl; acc1.deposit(1000); cout << "New balance acc1: " << acc1.account::getBalance () << endl; cout << "Balance (second constructor): "; cout << acc2.getBalance () << endl; acc2.withdraw(1000); cout << "New balance acc2: " << acc2.getBalance () << endl; }// end main // -----------------------------------------------