findacct
是函數的壞榜樣,一個類裏面移動,因爲沒有在一個特定的帳戶做任何事情,在幾個ACCO只是搜索UNTS。
您可以將BankAccount
類中的這種類型的函數作爲static
成員函數移動,但靜態成員函數與帶有奇特名稱的全局函數沒有多大區別。
對於面向對象方法更有意思的是,將移動一些作用於BankAccount
類中的特定銀行帳戶的函數,例如,像從
bool withdraw(int account_id, int amount)
更改爲
bool BankAccount::withdraw(int amount)
在下面的示例代碼中,我存儲的所有帳戶在一個私人的載體。要創建一個新帳戶,您必須調用一個提供ID號的靜態類函數。請注意,這段代碼包含許多微妙之處,除非您完全理解它,否則可能會有危險作爲基礎......這不幸是C++的一個特性,其中顯然邏輯語句可能在邏輯上起作用,或者可能會使計算機出現奇怪行爲。我的建議是抓住一本好的C++書,並在閱讀之前或在嘗試使用該語言時閱讀它。
C++過於複雜,有時並不合邏輯(由於歷史原因),只是通過實驗來學習它。
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
class BankAccount
{
private:
int id; // Unique identifier for this account
int balance; // How much money is present on this account
static std::vector<BankAccount*> accounts; // Global list of valid accounts
// Constructor - private!
BankAccount(int id) : id(id), balance(0)
{
// Add to global accounts list
accounts.push_back(this);
}
// Destructor - also private
~BankAccount()
{
// Remove from global accounts list
accounts.erase(std::find(accounts.begin(), accounts.end(),
this));
}
public:
// Public global function to create a new account
static bool createAccount(int id)
{
if (find(id) != NULL)
{
return false;
}
else
{
new BankAccount(id);
return true;
}
}
// This is a global function that given the unique identifiers
// returns a pointer to the account or NULL if non-existent
static BankAccount *find(int id)
{
for (int i=0,n=accounts.size(); i<n; i++)
if (accounts[i]->getId() == id)
return accounts[i];
return NULL;
}
// This is a global function that transfers money from one
// account to another and returns true if the operation is
// successful (i.e. if the source account has enough money)
static bool transfer(int from_id, int to_id, int amount)
{
BankAccount *from = find(from_id);
BankAccount *to = find(to_id);
if (from != NULL && // Is first account valid?
to != NULL && // Is second account valid?
from->withdraw(amount)) // Is there enough money?
{
to->deposit(amount); // move the withdrawn money
return true;
}
else
{
// Operation did not succeed
return false;
}
}
// Returns the id of the account
int getId()
{
return id;
}
// Returns the current balance for the account
int getBalance()
{
return balance;
}
// Deposit a sum on the bank account
void deposit(int amount)
{
balance += amount;
}
// Tries to withdraw the specified amount from the account
bool withdraw(int amount)
{
if (amount <= balance)
{
balance -= amount;
return true;
}
else
{
return false;
}
}
};
// This is also needed; the declaration of accounts inside
// the class (.h) doesn't actually allocate the vector... simply
// tells that the following line will be present in a .cpp
std::vector<BankAccount*> BankAccount::accounts;
///////////////////////////////////////////////////////////////////////////////////
// Do some test...
#define check(x) \
do { printf("check: %s\n", #x); if (!(x)) { \
printf("*** Fatal error (line %i)", __LINE__); \
exit(1); }}while(0)
int main(int argc, const char *argv[])
{
check(BankAccount::createAccount(6502) == true);
check(BankAccount::createAccount(386) == true);
// duplicate account!
check(BankAccount::createAccount(6502) == false);
// Not enough founds
check(BankAccount::transfer(386, 6502, 1000) == false);
// Deposit
BankAccount *p386 = BankAccount::find(386);
check(p386 != NULL);
p386->deposit(1000);
// Valid and invalid transfers...
check(BankAccount::transfer(386, 6502, 1000) == true); // ok
check(BankAccount::transfer(386, 6502, 1000) == false); // No more funds
check(BankAccount::transfer(6502, 386, 500) == true); // Give some back
check(BankAccount::transfer(386, 6502, 1000) == false); // Not enough funds
check(BankAccount::transfer(386, 6502, 400) == true); // ok
check(BankAccount::find(386)->getBalance() == 100);
check(BankAccount::find(6502)->getBalance() == 900);
return 0;
}
獲得'{int main()}'編譯的榮譽 – fizzer 2011-03-06 10:21:59