0% found this document useful (0 votes)
11 views2 pages

C++ BankAccount Class Example

Uploaded by

saptajyotinath65
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

C++ BankAccount Class Example

Uploaded by

saptajyotinath65
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a C++ program to demonstrate encapsulation using a BankAccount class.

#include <iostream>
using namespace std;

class BankAccount {
private:
long long accountNumber;
string holderName;
double balance;
public:
void openAccount(long long accNo, string name, double initial) {
accountNumber = accNo;
holderName = name;
balance = initial;
}
void deposit(double amount) {
balance += amount;
cout << "Amount Deposited Successfully!\n";
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
cout << "Amount Withdrawn Successfully!\n";
} else {
cout << "Insufficient Balance!\n";
}
}
void showDetails() {
cout << "\n--- Account Details ---\n";
cout << "Account No: " << accountNumber << endl;
cout << "Holder Name: " << holderName << endl;
cout << "Balance: " << balance << endl;
}
};

int main() {
BankAccount b;
int choice;
long long accNo;
string name;
double amount, initial;

cout << "Enter Account No (12–16 digits): ";


cin >> accNo;
cout << "Enter Name: ";
[Link]();
getline(cin, name);
cout << "Enter Initial Deposit: ";
cin >> initial;

[Link](accNo, name, initial);

do {
cout << "\n--- Menu ---\n";
cout << "1. Deposit\n";
cout << "2. Withdraw\n";
cout << "3. Show Details\n";
cout << "4. Exit\n";
cout << "Enter choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter amount to deposit: ";
cin >> amount;
[Link](amount);
break;
case 2:
cout << "Enter amount to withdraw: ";
cin >> amount;
[Link](amount);
break;
case 3:
[Link]();
break;
case 4:
cout << "Exiting program...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 4);

return 0;
}

You might also like