c++多态写一个ATM的存款取款
时间: 2024-06-14 11:04:25 浏览: 18
以下是一个使用C++多态实现ATM的存款和取款功能的示例:
```cpp
#include <iostream>
#include <vector>
class Account {
protected:
double balance;
public:
Account(double initialBalance) : balance(initialBalance) {}
virtual void deposit(double amount) = 0;
virtual void withdraw(double amount) = 0;
virtual void displayBalance() = 0;
};
class SavingsAccount : public Account {
public:
SavingsAccount(double initialBalance) : Account(initialBalance) {}
void deposit(double amount) override {
balance += amount;
std::cout << "Deposit successful. Current balance: " << balance << std::endl;
}
void withdraw(double amount) override {
if (balance >= amount) {
balance -= amount;
std::cout << "Withdrawal successful. Current balance: " << balance << std::endl;
} else {
std::cout << "Insufficient balance." << std::endl;
}
}
void displayBalance() override {
std::cout << "Current balance: " << balance << std::endl;
}
};
class CheckingAccount : public Account {
public:
CheckingAccount(double initialBalance) : Account(initialBalance) {}
void deposit(double amount) override {
balance += amount;
std::cout << "Deposit successful. Current balance: " << balance << std::endl;
}
void withdraw(double amount) override {
if (balance >= amount) {
balance -= amount;
std::cout << "Withdrawal successful. Current balance: " << balance << std::endl;
} else {
std::cout << "Insufficient balance." << std::endl;
}
}
void displayBalance() override {
std::cout << "Current balance: " << balance << std::endl;
}
};
int main() {
std::vector<Account*> accounts;
Account* account1 = new SavingsAccount(1000);
Account* account2 = new CheckingAccount(2000);
accounts.push_back(account1);
accounts.push_back(account2);
int choice;
double amount;
std::cout << "1. Deposit" << std::endl;
std::cout << "2. Withdraw" << std::endl;
std::cout << "3. Display Balance" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter amount to deposit: ";
std::cin >> amount;
accounts[0]->deposit(amount);
break;
case 2:
std::cout << "Enter amount to withdraw: ";
std::cin >> amount;
accounts[1]->withdraw(amount);
break;
case 3:
accounts[0]->displayBalance();
accounts[1]->displayBalance();
break;
default:
std::cout << "Invalid choice." << std::endl;
break;
}
delete account1;
delete account2;
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)