1
Question: You are tasked with implementing a simple banking system in
C++. Your program should incorporate the principles of object-oriented
programming discussed in class. Here are the requirements:
1. Define a class named BankAccount with the following attributes:
accountNumber (int): representing the account number of the bank account.
balance (double): representing the current balance of the bank account.
ownerName (string): representing the name of the account owner.
2. Implement the following member functions within the BankAccount class:
void deposit(double amount): adds the specified amount to the account balance.
void withdraw(double amount): subtracts the specified amount from the account
balance.
void displayAccountDetails(): displays the account details including account
number, balance, and owner name.
3. Create at least two objects of the BankAccount class and demonstrate the usage
of the member functions by performing deposit and withdrawal operations on these
objects.
Note: Ensure proper encapsulation and access specifiers are used. Use
appropriate constructors and destructors wherever necessary.
Program:-
#include <iostream>
#include <string>
using namespace std;
// BankAccount class definition
2
class BankAccount {
private:
int accountnumber;
double balance;
string ownername;
public:
// Constructor to initialize a BankAccount object
BankAccount(int accnum, double bal, string owner)
{
accountnumber=accnum;
balance=bal;
ownername=owner;
// Method to deposit money into the account
void deposit(double amount)
{
balance += amount;
cout << "Deposited $" << amount << " to account number: " <<
accountnumber <<endl;
}
3
// Method to withdraw money from the account
void withdraw(double amount)
{
if (amount > balance)
{
cout << "Withdrawal failed: Insufficient funds!"<<endl;
} else {
balance -= amount;
cout << "Withdrew $" << amount << " from account number: "
<< accountnumber << endl;
}
}
// Method to display account details
void displayAccountDetails()
{
cout << "Account Number: " << accountnumber<<endl;
cout<<"owner name: " << ownername<<endl;
cout<<"Current Balance: $" << balance << endl;
}
};
4
int main()
{
// User input for account creation
int accountnumber1,accountnumber2;
double balance1,balance2;
string ownername1,ownername2;
// Create first BankAccount object
cout << "Enter account number, balance, and owner name for the first
account:"<<endl;
cin >> accountnumber1 >> balance1;
cin.ignore(); // Ignore newline left in the input stream
getline(cin, ownername1);
BankAccount account1(accountnumber1, balance1, ownername1);
// Create second BankAccount object
cout << "Enter account number, balance, and owner name for the
second account:"<<endl;
cin >> accountnumber2 >> balance2;
cin.ignore(); // Ignore newline left in the input stream
getline(cin, ownername2);
BankAccount account2(accountnumber2, balance2, ownername2);
// Perform operations on the first account
5
double depositamount,withdrawamount;
cout << "Performing operations on the first account:"<<endl;
cout<<"Enter deposit amount for first account:";
cin>>depositamount;
account1.deposit(depositamount);
cout<<"Enter withdraw amount for first account:";
cin>>withdrawamount;
account1.withdraw(withdrawamount);
account1.displayAccountDetails();
// Perform operations on the second account
cout << "Performing operations on the second account:"<<endl;
cout<<"Enter deposit amount for second account:";
cin>>depositamount;
account2.deposit(depositamount);
cout<<"Enter withdraw amount for second account:";
cin>>withdrawamount;
account2.withdraw(withdrawamount);
account2.displayAccountDetails();
return 0;
}
6
Output: