Question: (Account Class) Create an Account class that a bank might useto repres
ID: 3616423 • Letter: Q
Question
Question: (Account Class) Create an Account class that a bank might useto represent customers' bank accounts. Include a data member oftype int to represent the account balance. [Note: In subsequentchapters, we'll use numbers that contain decimal points (e.g.,2.75) - called floating-point values - to represent dollaramounts.] Provide a constructor that receives an initial balanceand uses it to initialize the data member. The constructor shouldvalidate the initial balance to ensure that it's greater than orequal to 0. If not, set the balance to 0 and display an errormessage indicating that the initial balance was invalid. Providethree member functions. Member function credit should add an amountto the current balance. Member function debit should withdraw moneyfrom the Account and ensure that the debit amount does not exceedthe Account's balance. If it does, the balance should be leftunchanged and the function should print a message indicating "Debitamount exceeded account balance." Member function getbalance shouldreturn the current balance. Create a program that creates twoAccount objects and tests the member functions of class Account.Use a header file Account.h and a source code fileAccount.cpp. What is have so far....// Account.h
// Definition of Account c
class Account
{
public:
Account(int); // contructorinitializes balance
void credit(int); // add anamount to the accountbalance
void debit(int);
int getBalance(); // return the account balance
int getDebit();
private:
int balance; // data member thastores the blance
}; // end class account
// Account.cpp
// Member-function definition for class Account.
#include <iostream>
#include "Account.h"
using namespace std;
// Account constructorinitializes data member balance
Account::Account(intinitialBalance)
{
balance=0; // assume that theblance ebgins at 0
// if initialBalance is greater than 0, set this value as the
// balance of the Account; otherwise, balance remains 0
if( initialBlance > 0 )
balance = initialBlance;
// if initialBlance is negative, print error message
if( initialBlance < 0 )
cout<< "Error: Initial blance cannot be negative. <<endl;
} // end Account constructor
// credit(add) an amount to theaccount balnce
void Account::credit( int amount)
{
blance = blance = amount; // add amount to blance
}
/* write code to define member function debit. You have to write the dunction headerand two conditions. May take up to 7 lines. */
MISSING 2
// debit (subtract) an amountfrom the account balance
...
...
...// debit amount exceedsbalance
...
... //debit amount does not exceed balance
...
... //end function debit
...// return the accountbalance
int Account::getBlance()
{
return balance; // gives thevalue of balance to the callingfunction
}// end function getBalance
// function main begins program execution
int main()
{
Account account1( 50 ); //create Account object
Account account2( 25 ); //create Account object
// display initial balance ofeach object
cout << "account1 balance:$" << account1.getBalance()<<endl;
cout << "account2 balance:$" << account2.getBalance()<<endl;
int withdrawalAmount; // stores withdrawal amount read fromuser
cout << " Enter withdrawal amount for account1:"; //prompt
cin >> withdrawalAmount; // obtain user input
cout<< " attempting to subtract " << withdrawalAmount<< " from account1 balance ";
/* write code to withdraw money from account1 using functiondebit */
MISSING3
.... // try to subtract from account 1
// display balance
cout << "account1 balance:$" << account1.getBalance()<<endl;
cout << "account2 balance:$" << account2.getBalance()<<endl;
cout << " Enter withdrawal amount for account2:"; //prompt
cin >> withdrawalAmount; // obtain user input
cout<< " attempting to subtract " << withdrawalAmount<< " from account2 balance ";
/* write code to withdraw money from account2 using functiondebit */
MISSING 4
//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 Question: (Account Class) Create an Account class that a bank might useto represent customers' bank accounts. Include a data member oftype int to represent the account balance. [Note: In subsequentchapters, we'll use numbers that contain decimal points (e.g.,2.75) - called floating-point values - to represent dollaramounts.] Provide a constructor that receives an initial balanceand uses it to initialize the data member. The constructor shouldvalidate the initial balance to ensure that it's greater than orequal to 0. If not, set the balance to 0 and display an errormessage indicating that the initial balance was invalid. Providethree member functions. Member function credit should add an amountto the current balance. Member function debit should withdraw moneyfrom the Account and ensure that the debit amount does not exceedthe Account's balance. If it does, the balance should be leftunchanged and the function should print a message indicating "Debitamount exceeded account balance." Member function getbalance shouldreturn the current balance. Create a program that creates twoAccount objects and tests the member functions of class Account.Use a header file Account.h and a source code fileAccount.cpp. What is have so far....
// Account.h
// Definition of Account c
class Account
{
public:
Account(int); // contructorinitializes balance
void credit(int); // add anamount to the accountbalance
void debit(int);
int getBalance(); // return the account balance
int getDebit();
private:
int balance; // data member thastores the blance
}; // end class account
// Account.cpp
// Member-function definition for class Account.
#include <iostream>
#include "Account.h"
using namespace std;
// Account constructorinitializes data member balance
Account::Account(intinitialBalance)
{
balance=0; // assume that theblance ebgins at 0
// if initialBalance is greater than 0, set this value as the
// balance of the Account; otherwise, balance remains 0
if( initialBlance > 0 )
balance = initialBlance;
// if initialBlance is negative, print error message
if( initialBlance < 0 )
cout<< "Error: Initial blance cannot be negative. <<endl;
} // end Account constructor
// credit(add) an amount to theaccount balnce
void Account::credit( int amount)
{
blance = blance = amount; // add amount to blance
}
/* write code to define member function debit. You have to write the dunction headerand two conditions. May take up to 7 lines. */
MISSING 2
// debit (subtract) an amountfrom the account balance
...
...
...// debit amount exceedsbalance
...
... //debit amount does not exceed balance
...
... //end function debit
...// return the accountbalance
int Account::getBlance()
{
return balance; // gives thevalue of balance to the callingfunction
}// end function getBalance
// function main begins program execution
int main()
{
Account account1( 50 ); //create Account object
Account account2( 25 ); //create Account object
// display initial balance ofeach object
cout << "account1 balance:$" << account1.getBalance()<<endl;
cout << "account2 balance:$" << account2.getBalance()<<endl;
int withdrawalAmount; // stores withdrawal amount read fromuser
cout << " Enter withdrawal amount for account1:"; //prompt
cin >> withdrawalAmount; // obtain user input
cout<< " attempting to subtract " << withdrawalAmount<< " from account1 balance ";
/* write code to withdraw money from account1 using functiondebit */
MISSING3
.... // try to subtract from account 1
// display balance
cout << "account1 balance:$" << account1.getBalance()<<endl;
cout << "account2 balance:$" << account2.getBalance()<<endl;
cout << " Enter withdrawal amount for account2:"; //prompt
cin >> withdrawalAmount; // obtain user input
cout<< " attempting to subtract " << withdrawalAmount<< " from account2 balance ";
/* write code to withdraw money from account2 using functiondebit */
MISSING 4
//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
Explanation / Answer
/*****************Account.h *****************************/ // Account.h // Definition of Account c class Account { public: Account(int); // contructor initializes balance void credit(int); // add an amount to the account balance void debit(int); int getBalance(); // return the account balance int getDebit(); private: int balance; // data member tha stores the blance }; // end class account /************************************************/ /************Account.cpp **************************/ // Account.cpp // Member-function definition for class Account. #include #include "Account.h" using namespace std; /*Account constructor initializes data member balance */ Account::Account(int initialBalance) { balance=0; /** assume that the blance ebginsat 0 if initialBalance is greaterthan 0, set this value as the balance of the Account;otherwise, balance remains 0 */ if( initialBalance > 0 ) balance = initialBalance; /* if initialBlance is negative, print errormessage */ if( initialBalance < 0 ) coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.