Call us at 0700-922-6559 or Click here

Define a class to represent a bank account which includes the following members as Data members: a) Name of the depositor b) Account Number c) Withdrawal amount d) Balance amount in the accountMember Functions: a) To assign initial values b)To deposit an amount c) To withdraw an amount after checking the balance d) To display name and balance.

#include <iostream>

#include <string>

using namespace std;

// Class to represent a bank account

class BankAccount {

private:

    string name;    // Name of the depositor

    int accountNo;  // Account number

    float balance;  // Balance amount

public:

    // Function to assign initial values

    void initialize(string name, int accountNo, float initialBalance) {

        this->name = name;

        this->accountNo = accountNo;

        this->balance = initialBalance;

    }

    // Function to deposit an amount

    void deposit(float amount) {

        balance += amount;

        cout << amount << ” rupees deposited successfully.\n”;

    }

    // Function to withdraw an amount

    void withdraw(float amount) {

        if (amount <= balance) {

            balance -= amount;

            cout << amount << ” rupees withdrawn successfully.\n”;

        } else {

            cout << “Insufficient balance.\n”;

        }

    }

    // Function to display name and balance

    void display() {

        cout << “Name: ” << name << endl;

        cout << “Account Number: ” << accountNo << endl;

        cout << “Balance: ” << balance << endl;

    }

};

int main() {

    BankAccount account;  // Create a bank account object

    // Assign initial values using Indian names

    account.initialize(“Rohan Kumar”, 12345, 5000);

    // Perform transactions

    account.deposit(2000);

    account.withdraw(3500);

    // Display account details

    account.display();

    return 0;

}

Output:

2000 rupees deposited successfully.

3500 rupees withdrawn successfully.

Name: Rohan Kumar

Account Number: 12345

Balance: 3500

Here’s a step-by-step explanation of the code:

1. Headers and Namespace:

  • #include <iostream>: Includes the iostream header for input/output operations.
  • #include <string>: Includes the string header for string manipulation.
  • using namespace std;: Brings the std namespace into scope for convenient use of its elements.

2. BankAccount Class:

  • class BankAccount { … }: Defines a class named BankAccount to represent a bank account.
  • private: Encapsulates data members to protect them from direct access outside the class.
  • string name;: Stores the account holder’s name.
  • int accountNo;: Stores the account number.
  • float balance;: Stores the account balance.
  • public: Contains member functions accessible from outside the class.
  • void initialize(string name, int accountNo, float initialBalance) { … }: Sets initial values for the account attributes.
  • void deposit(float amount) { … }: Deposits an amount into the account.
  • void withdraw(float amount) { … }: Withdraws an amount from the account, checking for sufficient balance.
  • void display() { … }: Displays the account holder’s name and balance.

3. Main Function:

  • int main(): The main function where program execution begins.
  • BankAccount account;: Creates an object of the BankAccount class, representing a specific bank account.
  • account.initialize(“Rohan Kumar”, 12345, 5000);: Initializes the account with the given name, account number, and initial balance.
  • account.deposit(2000);: Deposits 2000 rupees into the account.
  • account.withdraw(3500);: Attempts to withdraw 3500 rupees from the account.
  • account.display();: Displays the account details (name and balance).
  • return 0;: Indicates successful program termination.

Leave a Reply

Your email address will not be published. Required fields are marked *