Pages

Wednesday, June 4, 2025

Bank Account Question (University OOPs Lab Topic)

 #include <iostream>

using namespace std;

class BankAccount {
private:
    int accountNumber;
    double balance;
   
public:    
// Constructor to initialize account    
    BankAccount(int accNo, double initialBalance = 0.0) {
        accountNumber = accNo;
        balance = initialBalance;
    }
   
   
    void deposit(double amount) {
        if(amount > 0) {
            balance += amount;
             cout << "Deposited: " << amount << endl;
        } else {
            cout << "Invalid deposit amount!" << endl;
        }
    }
    void withdraw(double amount) {
        if(amount > 0 && amount <= balance) {
            balance -= amount;
            cout << "Withdrawn: " << amount<< endl;
        } else {
            cout << "Invalid or insufficient funds for withdrawal!" << endl;
        }
    }
   
    // method to get balance
    double getBalance() const {
        return balance;
    }
   
    // Optional: Show account number and balance
    void showAccountInfo() const {
        cout << "Account Number: " << accountNumber << endl;
        cout << "Current Balance: " << balance << endl;
    }
   
   
};

int main() {
    // Create a bank with account number 1001 and initial balance of 500
   
    BankAccount account(1001, 500.0);
   
    account.showAccountInfo();
   
    // Perform deposit and withdrawal operations
    account.deposit(250);
    account.withdraw(100);
   
    //Show final balance
    cout << "Final balance: " << account.getBalance() << endl;
   
    return 0;
}

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...