Call us at 0700-922-6559 or Click here

Write the above program for handling n number of account holders using array of objects.

#include <iostream>

#include <string>

using namespace std;

// Class definition (same as before)

class BankAccount {

   // … (same as previous code)

};

int main() {

   int n;

   cout << “Enter the number of account holders: “;

   cin >> n;

   BankAccount accounts[n];  // Array to hold n bank account objects

   // Get details for each account holder

   for (int i = 0; i < n; i++) {

       string name;

       int accountNo;

       float initialBalance;

       cout << “\nEnter details for account holder ” << i + 1 << “:\n”;

       cout << “Name: “;

       cin >> name;

       cout << “Account Number: “;

       cin >> accountNo;

       cout << “Initial Balance: “;

       cin >> initialBalance;

       accounts[i].initialize(name, accountNo, initialBalance);  // Initialize each account

   }

   // Perform transactions and display details for each account

   for (int i = 0; i < n; i++) {

       // Perform transactions (example)

       accounts[i].deposit(1000);

       accounts[i].withdraw(500);

       // Display account details

       cout << “\nAccount ” << i + 1 << ” details:\n”;

       accounts[i].display();

   }

   return 0;

}

Output:

Enter the number of account holders: 2

Enter details for account holder 1:

Name: Radha Gupta

Account Number: 1001

Initial Balance: 5000

Enter details for account holder 2:

Name: Mahesh Sharma

Account Number: 1002

Initial Balance: 3000

1000 rupees deposited successfully.

500 rupees withdrawn successfully.

Account 1 details:

Name: Radha Gupta

Account Number: 1001

Balance: 5500

1000 rupees deposited successfully.

500 rupees withdrawn successfully.

Account 2 details:

Name: Mahesh Sharma

Account Number: 1002

Balance: 3500

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

1. Headers, Namespace, and Class:

  • Same as the previous code, defining the BankAccount class.

2. Main Function:

  • int main(): The program’s entry point.
    • int n; Declares an integer variable n to store the number of account holders.
    • cout << “Enter the number of account holders: “; cin >> n; Prompts the user to enter the number of accounts and stores it in n.
    • BankAccount accounts[n]; Creates an array named accounts to hold n objects of the BankAccount class.

3. Getting Account Details:

  • for (int i = 0; i < n; i++) { … }: Iterates n times to get details for each account holder.
    • string name; int accountNo; float initialBalance; Declares variables to store account details.
    • cout << “\nEnter details for account holder ” << i + 1 << “:\n”; Prompts for details.
    • cin >> name; cin >> accountNo; cin >> initialBalance; Reads input from the user.
    • accounts[i].initialize(name, accountNo, initialBalance); Initializes the i-th account object in the accounts array with the provided details.

4. Performing Transactions and Displaying Details:

  • for (int i = 0; i < n; i++) { … }: Iterates through each account object in the array.
    • accounts[i].deposit(1000); accounts[i].withdraw(500); Performs example transactions (deposit and withdrawal) on each account.
    • cout << “\nAccount ” << i + 1 << ” details:\n”; accounts[i].display(); Prints account details for each account.

5. Return Statement:

  • return 0; Indicates successful program termination.

Leave a Reply

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