Question 03 of 03

Bank Account Class with Deposit, Withdrawal and Balance Validation

Write a Turbo C++ program to create a BankAccount class with private member variables (accNo, name, balance) and public member functions (openAccount, deposit, withdraw, displayBalance).

C++
#include <iostream.h>
#include <conio.h>
#include <string.h>

class BankAccount {
private:
    long accNo;
    char name[40];
    float balance;

public:
    void openAccount(long acc, char holderName[], float initialDeposit) {
        accNo = acc;
        strcpy(name, holderName);
        balance = initialDeposit;
        cout << "Account successfully opened for " << name << "!" << endl;
    }

    void deposit(float amount) {
        if (amount > 0) {
            balance += amount;
            cout << "Deposited Rs. " << amount << ". New Balance = Rs. " << balance << endl;
        } else {
            cout << "Invalid deposit amount." << endl;
        }
    }

    void withdraw(float amount) {
        if (amount > balance) {
            cout << "Transaction Failed! Insufficient Balance. Current Balance: Rs. " << balance << endl;
        } else if (amount <= 0) {
            cout << "Invalid withdrawal amount." << endl;
        } else {
            balance -= amount;
            cout << "Withdrawn Rs. " << amount << ". Remaining Balance = Rs. " << balance << endl;
        }
    }

    void display() {
        cout << "\n--- Account Statement ---" << endl;
        cout << "Account Number : " << accNo << endl;
        cout << "Account Holder : " << name << endl;
        cout << "Current Balance: Rs. " << balance << endl;
    }
};

void main() {
    BankAccount myAcc;
    int choice;
    float amt;

    clrscr();

    myAcc.openAccount(100200300, "Rahul Sharma", 5000.0);

    do {
        cout << "\n1. Deposit\n2. Withdraw\n3. Display Balance\n4. Exit" << endl;
        cout << "Enter Choice (1-4): ";
        cin >> choice;

        switch (choice) {
            case 1:
                cout << "Enter amount to deposit: ";
                cin >> amt;
                myAcc.deposit(amt);
                break;
            case 2:
                cout << "Enter amount to withdraw: ";
                cin >> amt;
                myAcc.withdraw(amt);
                break;
            case 3:
                myAcc.display();
                break;
            case 4:
                cout << "Exiting banking system. Thank you!" << endl;
                break;
            default:
                cout << "Invalid choice! Select 1-4." << endl;
        }
    } while (choice != 4);

    getch();
}
Question 3 of 3