Knowee
Questions
Features
Study Tools

Write a C++ program to implement a class called BankAccount that represents a simple bank account. The class should have private member variables for account number and balance. Include member functions to deposit and withdraw money from the account.Testcase:Input:Rs.1000.0  // initial balanceRs.500.0  // depositRs.200.0   // withdrawOutput:Rs.1300.0  // final balance Input:Rs.2000.0  // initial balanceRs.1000.0  // depositRs.4000.0   // withdrawOutput:NA  // final balance

Question

Write a C++ program to implement a class called BankAccount that represents a simple bank account. The class should have private member variables for account number and balance. Include member functions to deposit and withdraw money from the account.Testcase:Input:Rs.1000.0  // initial balanceRs.500.0  // depositRs.200.0   // withdrawOutput:Rs.1300.0  // final balance Input:Rs.2000.0  // initial balanceRs.1000.0  // depositRs.4000.0   // withdrawOutput:NA  // final balance

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple C++ program that implements a BankAccount class with deposit and withdraw functions:

#include<iostream>
using namespace std;

class BankAccount {
    private:
        int accountNumber;
        double balance;

    public:
        BankAccount(int accountNumber, double initialBalance) {
            this->accountNumber = accountNumber;
            this->balance = initialBalance;
        }

        void deposit(double amount) {
            balance += amount;
        }

        bool withdraw(double amount) {
            if(amount > balance) {
                cout << "Insufficient balance." << endl;
                return false;
            } else {
                balance -= amount;
                return true;
            }
        }

        double getBalance() {
            return balance;
        }
};

int main() {
    BankAccount account(1, 1000.0); // initial balance
    account.deposit(500.0); // deposit
    if(account.withdraw(200.0)) { // withdraw
        cout << "Final balance: Rs." << account.getBalance() << endl;
    }

    BankAccount account2(2, 2000.0); // initial balance
    account2.deposit(1000.0); // deposit
    if(!account2.withdraw(4000.0)) { // withdraw
        cout << "Withdrawal amount exceeds the current balance." << endl;
    }

    return 0;
}

In this program, the BankAccount class has two private member variables: accountNumber and balance. The class has a constructor that initializes these variables, and three member functions: deposit, withdraw, and getBalance. The deposit function adds the deposit amount to the balance. The withdraw function checks if the withdrawal amount is greater than the balance. If it is, it prints an error message and returns false; otherwise, it subtracts the withdrawal amount from the balance and returns true. The getBalance function returns the current balance.

The main function creates two BankAccount objects with initial balances of Rs.1000.0 and Rs.2000.0, respectively. It then deposits Rs.500.0 into the first account and Rs.1000.0 into the second account. It attempts to withdraw Rs.200.0 from the first account and Rs.4000.0 from the second account. If the withdrawal is successful, it prints the final balance; otherwise, it prints an error message.

This problem has been solved

Similar Questions

Write a C++ program to implement a class called BankAccount that represents a simple bank account. The class should have private member variables for account number and balance. Include member functions to deposit and withdraw money from the accoun

Write a C++ program to implement a class called Bank that has private member variables for account_number, operation_type,old_balance , amount, and  new_balance.Include member function “calculate “that prints the balance after deposit or withdrawal.Sample output1011 (account number)D  (D-deposit / W-withdraw)1000 (old balance)2000 (amount)Sample Output3000

You are required to implement a simple C++ program for managing a bank account. The program should include a class BankAccount with private attributes for account number, account holder name, and account balance.The BankAccount class should have the following:A parameterized constructor to initialize the account number, account holder name, and account balance.A friend function displayAccountInfo that takes a BankAccount object as an argument and displays the account information (account number, account holder name, and account balance).Additionally, the program should check if the account balance is greater than Rs.10,000 and display whether the account holder is eligible for a credit card.Testcase1:Sample Input:12345   // account numberJohn Doe  // name15000   // balanceSample Output:12345John Doe15000Yes Testcase2:Sample Input:98765   // account numberVirat Kohli  // name8000   // balanceSample Output:98765Virat Kohli8000No

Program that creates a class Account that stores a variable balance. The class has methods to startaccount, to deposit money, to withdraw money and tell the current balance amoun

A program to simulate a simple banking system in which the initial balance and rate of interest are read from the keyboard and these values are initialised using the constructor member function. The program consists of the following methods:To initialise the balance amount and the rate of interest using constructor member functionTo make depositTo withdraw an amount for the balanceTo find compound interest based on the rate of interestTo know the balance amountTo display the menu options

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.