UML类图:

代码:
| #include <iostream> |
| #include <iomanip> |
| using namespace std; |
| |
| |
| class Account { |
| public: |
| Account() :balance(0) {} |
| Account(double balance) :balance(balance) {} |
| |
| |
| virtual bool debit(double amount); |
| virtual bool credit(double amount); |
| virtual double getBalance(); |
| virtual ~Account() {} |
| private: |
| double balance; |
| }; |
| |
| bool Account::debit(double amount) { |
| if (amount > balance) { |
| cout << "Debit amount exceeded account balance." << endl; |
| return false; |
| } else { |
| this->balance = this->balance - amount; |
| return true; |
| } |
| } |
| |
| bool Account::credit(double amount) { |
| if (amount < 0) { |
| return false; |
| } else { |
| this->balance = this->balance + amount; |
| return true; |
| } |
| } |
| |
| double Account::getBalance() { |
| return this->balance; |
| } |
| |
| |
| class SavingsAccount :public Account { |
| public: |
| |
| SavingsAccount(double balance, double rate) :Account(balance), interestRate(rate / 100) {} |
| double getInterestRate() { |
| return this->interestRate; |
| } |
| |
| double calculateInterest() { |
| return this->getBalance() * this->interestRate; |
| } |
| ~SavingsAccount() {} |
| private: |
| double interestRate; |
| }; |
| |
| |
| class CheckAccount :public Account { |
| public: |
| |
| CheckAccount(double balance, double fee) :Account(balance), fee(fee) {} |
| double getFee() { |
| return this->fee; |
| } |
| |
| bool debit(double amount) { |
| if (amount > this->getBalance()) { |
| cout << "Debit amount exceeded account balance." << endl; |
| return false; |
| } else if (this->fee + amount > this->getBalance()) { |
| cout << "Transaction fee exceeded account balance while debiting." << endl; |
| return false; |
| } else { |
| Account::debit(amount + this->fee); |
| return true; |
| } |
| } |
| |
| bool credit(double amount) { |
| if (this->fee > amount + this->getBalance()) { |
| cout << "Transaction fee exceeded account balance while crediting." << endl; |
| return false; |
| } else { |
| Account::credit(amount); |
| Account::debit(this->fee); |
| return true; |
| } |
| } |
| ~CheckAccount() {}; |
| private: |
| double fee; |
| }; |
| |
| int main() { |
| Account* accounts[3]; |
| accounts[0] = new SavingsAccount(100, 3); |
| accounts[1] = new CheckAccount(100, 5); |
| accounts[2] = new CheckAccount(50, 5); |
| |
| for (int i = 0; i < 3; i++) { |
| cout << "第" << i + 1 << "次循环的结果:" << endl; |
| accounts[i]->debit(200); |
| accounts[i]->debit(40); |
| accounts[i]->credit(50); |
| accounts[i]->debit(49); |
| accounts[i]->debit(43); |
| accounts[i]->credit(1); |
| SavingsAccount* derivedPtr = dynamic_cast<SavingsAccount*>(accounts[i]); |
| if (derivedPtr != NULL) { |
| derivedPtr->credit(derivedPtr->calculateInterest()); |
| } |
| cout << "账户的余额为:" << fixed << setprecision(2) << accounts[i]->getBalance() << endl; |
| } |
| |
| delete accounts[0]; |
| delete accounts[1]; |
| delete accounts[2]; |
| |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统