Answer to Question #226762 in C++ for Fairy

Question #226762

Define a class for a Bank account that includes the following data members: Name of the depositor, Account number, Type of account, Balance amount in the account.

The class also contains following member functions: 

A constructor to assign initial values.

Deposit function to deposit some amount. It should accept amount as parameter.

Withdraw function to withdraw an amount after checking the balance. It should accept the amount as parameter.

Display function to display name and balance.


1
Expert's answer
2021-08-17T12:55:04-0400
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Account {
public:
    enum Type {deposite, saving};
    Account(string name, int number, Type type, double init) ;
    void deposit(double amount);
    double withdraw(double amount);
    void display();

private:
    string name;
    int number;
    Type type;
    double balance;    
};

Account::Account(string aname, int anumber, Type atype, double init)
{
    name = aname;
    number = anumber;
    type = atype;
    balance = init > 0.0 ? init : 0;
}

void Account::deposit(double amount)
{
    if (amount < 0.0) {
        cout << "Can't deposite negative amount" << endl;
        amount = 0.0;
    }
    balance += amount;
}

double Account::withdraw(double amount) {
    if (amount < 0.0) {
        cout << "Can't withdraw negative amount" << endl;
        amount = 0.0;
    }
    if (amount > balance) {
        cout << "Not enough funds in the account withdraw only $"
             << fixed <<setprecision(2) << balance << endl;
        amount = balance;
    }
    balance -= amount;
    return amount;
}

void Account::display() {
    cout << name << ": $" << fixed << setprecision(2) << balance << endl;
}

int main() {
    Account account("Smith", 12345, Account::deposite, 100);

    account.deposit(1000);
    account.withdraw(200);
    account.display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS