Certainly! Here is an example of a highly protective bank program implemented in C++ using Object-Oriented Programming (OOP) principles:
#include <iostream>
#include <string>
class Bank {
private:
std::string username;
std::string password;
double accountBalance;
public:
Bank(const std::string& username, const std::string& password)
: username(username), password(password), accountBalance(0.0) {}
bool authenticate(const std::string& username, const std::string& password) {
if (this->username == username && this->password == password) {
std::cout << "Authentication successful!" << std::endl;
return true;
}
else {
std::cout << "Authentication failed. Invalid credentials." << std::endl;
return false;
}
}
void deposit(double amount) {
if (amount > 0) {
accountBalance += amount;
std::cout << "Deposit successful! Your new balance is: $" << accountBalance << std::endl;
}
else {
std::cout << "Invalid deposit amount." << std::endl;
}
}
void withdraw(double amount) {
if (amount > 0 && amount <= accountBalance) {
accountBalance -= amount;
std::cout << "Withdrawal successful! Your new balance is: $" << accountBalance << std::endl;
}
else {
std::cout << "Invalid withdrawal amount or insufficient funds." << std::endl;
}
}
void checkBalance() {
std::cout << "Your current balance is: $" << accountBalance << std::endl;
}
};
int main() {
Bank bank("myusername", "mypassword");
std::string username, password;
std::cout << "Enter your username: ";
std::cin >> username;
std::cout << "Enter your password: ";
std::cin >> password;
if (bank.authenticate(username, password)) {
bank.deposit(500.0);
bank.withdraw(200.0);
bank.checkBalance();
}
return 0;
}
In the above code, we have implemented a basic banking program with authentication, deposit, withdrawal, and balance checking features. Here's a breakdown of the code:
- The
Bank class encapsulates the banking functionalities. It has private member variables for storing the username, password, and account balance.
- The constructor is used to initialize the username, password, and account balance when creating a
Bank object.
- The
authenticate function compares the provided username and password with the stored ones to authenticate the user.
- The
deposit function allows the user to add funds to their account balance.
- The
withdraw function allows the user to deduct funds from their account balance, provided they have enough funds.
- The
checkBalance function displays the current account balance.
- In the
main function, we create a Bank object and prompt the user to enter their credentials. If the authentication is successful, we demonstrate the deposit, withdrawal, and balance checking features.
This is just a basic example, and you can extend the program by adding more features like transaction history, interest calculations, multiple accounts, etc. Additionally, make sure to implement proper security measures like secure data storage, encryption, and input sanitation to enhance the protection of the bank program.