Keywords: doc

Next: MICO application Up: Sample Program Previous: Sample Program

## Standalone program

Imagine a bank which maintains accounts of its customers. An object which implements such a bank account offers three operations: deposit a certain amount of money, withdraw a certain amount of money, and an operation called balance that returns the current account balance. The state of an account object consists of the current balance. The following C++ code fragment shows the class declaration for such an account object:

      class Account {
          long _current_balance;
      public:
          Account ();
          void deposit (unsigned long amount);
          void withdraw (unsigned long amount);
          long balance ();
      };

The above class declaration describes the interface and the state of an account object, the actual implementation which reflects the behavior of an account, is shown below:

      Account::Account ()
      {
          _current_balance = 0;
      }
      void Account::deposit (unsigned long amount)
      {
          _current_balance += amount;
      }
      void Account::withdraw (unsigned long amount)
      {
          _current_balance -= amount;
      }
      long Account::balance ()
      {
          return _current_balance;
      }

Here is a piece of code that makes use of a bank account:

      #include <iostream.h>

      int main (int argc, char *argv[])
      {
          Account acc;

          acc.deposit (700);
          acc.withdraw (250);
          cout << "balance is " << acc.balance() << endl;

          return 0;
      }

Since a new account has the initial balance of 0, the above code will print out ``balance is 450''.