Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://rtm-cs.sinp.msu.ru/manual/mico/doc/node13.html
Дата изменения: Mon Jun 7 21:54:57 1999 Дата индексирования: Mon Oct 1 21:21:19 2012 Кодировка: |
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 , the above code will print out ``balance is 450''.