用c++设计一个Bank类,实现银行某账号的资金往来账目管理。程序要求完成以下操作: (1) 创建账户:账户包括账号、创建日期、账户余额(创建账户时存入的钱数)。 (2) 存钱:执行存钱操作,更新账户余额 (3) 取钱:执行取钱操作,更新账户余额。
时间: 2024-10-13 11:04:55 浏览: 100
在C++中,我们可以设计一个简单的Bank类来实现银行账户的管理。这里是一个基础的实现示例:
```cpp
#include <iostream>
#include <string>
class BankAccount {
private:
std::string accountNumber;
std::string creationDate;
double balance;
public:
// 构造函数
BankAccount(const std::string& accountNum, const std::string& date, double initialBalance) :
accountNumber(accountNum), creationDate(date), balance(initialBalance) {}
// 获取账户信息
void displayInfo() const {
std::cout << "Account Number: " << accountNumber << "\n";
std::cout << "Creation Date: " << creationDate << "\n";
std::cout << "Current Balance: " << balance << "\n";
}
// 存钱操作
void deposit(double amount) {
if (amount > 0) {
balance += amount;
std::cout << "Deposited: $" << amount << ". New Balance: $" << balance << "\n";
} else {
std::cout << "Invalid deposit amount.\n";
}
}
// 取钱操作
bool withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
std::cout << "Withdrawn: $" << amount << ". New Balance: $" << balance << "\n";
return true; // 成功操作返回true
} else {
std::cout << "Insufficient funds.\n";
return false; // 资金不足返回false
}
}
};
int main() {
BankAccount myAccount("123456", "2023-01-01", 1000);
myAccount.displayInfo();
// 示例操作
myAccount.deposit(500);
myAccount.withdraw(700);
return 0;
}
```
这个BankAccount类包含私有成员变量accountNumber、creationDate和balance,以及对应的构造函数、displayInfo方法、deposit和withdraw方法。在main函数中,我们创建了一个账户并进行了存钱和取钱的操作。
阅读全文