Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

simple C++ Account Class reate an Account class that a bank might use to represe

ID: 3664174 • Letter: S

Question

simple C++

Account Class reate 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: 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 initialize 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 Ac- count 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 exceeded account balance Member function getBalance should return the current Account objects and tests the member functions of balance. Create a program that creates two class Account

Explanation / Answer

#include <string>

using std::string;

class Account

{

    public:

        Account(int);

        void credit(int);   

        void debit(int);

        int getBalance();

         

    private:

        int balance;       

};

</string>

#include <iostream>

using std::cout;

using std::endl;

#include "Account.h"

Account::Account(int initAmount)

{

    if(initAmount < 0)

    {

        balance = 0;

        cout << "ERROR!!! The initial balance was invalid. ";               

    }

    else

        balance = initAmount;

     

}

void Account::credit(int amount)

{

    balance += amount;

}

void Account::debit(int amount)

{

    if(amount > balance)

    {

        cout << "ERROR!!! Debit amount exceeded account balance. ";

        return;

    }

    balance -= amount;

}

int Account::getBalance()

{

    return balance;

}

</iostream>

#include <iostream>

using std::cout;

using std::endl;

#include "Account.h"

int main(int argc, char *argv[])

{

    Account acct1(-10);

    cout << "Acct 1, Balance => $" << acct1.getBalance() << " ";

    acct1.credit(100);

    cout << "Acct 1, Balance => $" << acct1.getBalance() << " ";

     

     

    Account acct2(1000);   

    cout << "Acct 2, Balance => $" << acct2.getBalance() << " ";

    acct2.debit(2000);

    cout << "Acct 2, Balance => $" << acct2.getBalance() << " ";

         

    system("PAUSE");

    return EXIT_SUCCESS;

}

</iostream>