Member Functions in C++ language
This is not the process a normal bank uses to handle withdrawals. In a perfect world where everyone is honest and makes no mistakes, all is well. If I place the bills in my wallet with other money already there, I may never detect the error. Clearly a bank needs a more controlled procedure for handling customer withdrawals.
class Account {
public:
// String representing the name of the account's owner
string name;
// The account number
int id;
// The current account balance
double balance;
};
bool withdraw(Account & acct, double amt) {
bool result = false; // Unsuccessful by default
if (acct.balance - amt >= 0) {
acct.balance -= amt;
result = true; // Success
}
return result;
}
// Good client code
// ----------------
// A simple database of bank customers
std::vector < Account > accountDB;
// Populate the database via some function
accountDB = initialize_db();
// Get transaction information
int number;
double debit;
std::cout << "Please enter account number and amount to withdraw:";
std::cin >> number >> debit;
// Locate account index
int n = accountDB.size(), i = 0;
while (i < n) {
if (accountDB[i].id == number)
break; // Found the account
i++;
}
// Perform the transaction, if possible
if (i < n) {
if (withdraw(accountDB[i], debit))
std::cout << "Withdrawal successful\n";
else
std::cout << "Cannot perform the withdraw\n";
} else
std::cout << "Account does not exist\n";
// Perform the transaction
if (i < n)
accountDB[i] -= debit; // What if debit is too big?
else
std::cout << "Account does not exist\n";
class Account {
// String representing the name of the account's owner
string name;
// The account number
int id;
// The current account balance
double balance;
public:
// Methods will be added here . . .
};
all the fields no longer reside in the public section of the class definition.
class Account {
private:
// String representing the name of the account's owner
string name;
// The account number
int id;
// The current account balance
double balance;
public:
// Methods will be added here . . .
};
class Account {
// String representing the name of the account's owner
string name;
// The account number
int id;
// The current account balance
double balance;
public:
/*******************************************************
* deposit(amt)
* Adds amount amt to the account's balance.
*
* Author: Sam Coder
* Date: April 17, 2017
*******************************************************/
void deposit(double amt) {
balance += amt;
}
/*******************************************************
* withdraw(amt)
* Deducts amount amt from the account's balance,
* if possible.
* Returns true if successful; otherwise, it returns false.
* A call can fail if the withdraw would
* cause the balance to fall below zero
*
* amt: funds to withdraw
*
* Author: Sam Coder
* Date: April 17, 2017
*******************************************************/
bool withdraw(double amt) {
bool result = false; // Unsuccessful by default
if (balance - amt >= 0) {
balance -= amt;
result = true; // Success
}
return result;
}
};
// Withdraw money from the Account object named acct
acct.withdraw(100.00);
The withdraw method definition uses three variables: amt, result, and balance. Where is balance declared? It is the field declared in the private section of the class.
// Affects the balance field of acct1 object
acct1.withdraw(100.00);
// Affects the balance field of acct2 object
acct2.withdraw(25.00);
It is important to note that the compiler checks in this order. That means it is legal to give a parameter to a method or a local variable the same name as an instance variable within the class. In this case the method’s code cannot access the instance variable or global variable by using its simple name. We say the local variables hide the instance or global variables from view
Methods may be overloaded just like global functions (see Section 10.3). Recall that a function’s signature consists of its name and parameter types; a method’s signature too consists of its name and parameter types.
int count;
public:
// Allow clients to reset the counter to zero
void clear() {
count = 0;
}
// Allow clients to increment the counter
void inc() {
count++;
}
// Return a copy of the counter's current value
int get() {
return count;
}
};
For this code we see that each Counter object will store a single integer value (its count field). Under Visual C++ sizeof Counter is the same as sizeof int—that is, four. A local Counter object consumes four bytes of stack space, a global Counter object uses four bytes of static memory, and a dynamically-allocated Counter object uses four bytes of heap space.