(Account Class) Create an Account class that a bank might use to represent customers’ bank accounts. Include a data member of type int to represent the account balance. [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—called floating-point values— to represent dollar amounts.] Provide a constructor that receives an initial balance and uses it to
ini- tialize the data member. The constructor should validate the initial balance to ensure that it’s greater than or equal to 0. If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceed- ed account balance." Member function getBalance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.
What will be an ideal response?
```
// Definition of Account class.
class Account
{
public:
Account( int ); // constructor initializes balance
void credit( int ); // add an amount to the account balance
void debit( int ); // subtract an amount from the account balance
int getBalance(); // return the account balance
private:
int balance; // data member that stores the balance
}; // end class Account
```
```
// Member-function definitions for class Account.
#include
#include "Account.h" // include definition of class Account
using namespace std;
// Account constructor initializes data member balance
Account::Account( int initialBalance )
{
balance = 0; // assume that the balance begins at 0
// if initialBalance is greater than 0, set this value as the
// balance of the Account; otherwise, balance remains 0
if ( initialBalance > 0 )
balance = initialBalance;
// if initialBalance is negative, print error message
if ( initialBalance < 0 )
cout << "Error: Initial balance cannot be negative.\n" << endl;
} // end Account constructor
// credit (add) an amount to the account balance
void Account::credit( int amount )
{
balance = balance + amount; // add amount to balance
} // end function credit
// debit (subtract) an amount from the account balance
void Account::debit( int amount )
{
if ( amount > balance ) // debit amount exceeds balance
cout << "Debit amount exceeded account balance.\n" << endl;
if ( amount <= balance ) // debit amount does not exceed balance
balance = balance - amount;
} // end function debit
// return the account balance
int Account::getBalance()
{
return balance; // gives the value of balance to the calling function
} // end function getBalance
```
```
// Create and manipulate Account objects.
#include
#include "Account.h"
using namespace std;
// function main begins program execution
int main()
{
Account account1( 50 ); // create Account object
Account account2( 25 ); // create Account object
// display initial balance of each object
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
int withdrawalAmount; // stores withdrawal amount read from user
cout << "\nEnter withdrawal amount for account1: "; // prompt
cin >> withdrawalAmount; // obtain user input
cout << "\nattempting to subtract " << withdrawalAmount
<< " from account1 balance\n\n";
account1.debit( withdrawalAmount ); // try to subtract from account1
// display balances
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "\nEnter withdrawal amount for account2: "; // prompt
cin >> withdrawalAmount; // obtain user input
cout << "\nattempting to subtract " << withdrawalAmount
<< " from account2 balance\n\n";
account2.debit( withdrawalAmount ); // try to subtract from account2
// display balances
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
return 0; // indicate successful termination
} // end main
```
account1 balance: $50
account2 balance: $25
Enter withdrawal amount for account1: 35
attempting to subtract 35 from account1 balance
account1 balance: $15
account2 balance: $25
Enter withdrawal amount for account2: 50
attempting to subtract 50 from account2 balance
Debit amount exceeded account balance.
account1 balance: $15
account2 balance: $25
You might also like to view...
When you create methods with the same name but with different parameter lists, those methods are known as ____________________.
Fill in the blank(s) with the appropriate word(s).
Construct Element from atomic number
What will be an ideal response?
Which class of fence is for commercial usage?
A. Class 1 B. Class 2 C. Class 3 D. Class 4
Modems do not cost as much as network interface cards.
Answer the following statement true (T) or false (F)