c++多态继承写一个可以开户的银行管理系统
时间: 2023-12-24 12:05:38 浏览: 94
好的,我可以为您提供一个基于多态继承的银行管理系统示例,它可以用于开户、存款、取款、查询余额等操作。
首先,我们需要定义一个基类BankAccount,它包含一些通用属性和方法,如账户号、账户名、账户余额,以及存款、取款、查询余额等操作。然后,我们可以创建两个派生类CheckingAccount和SavingsAccount,用于处理不同类型的银行账户。
以下是一个简单的示例代码,其中包含了基类和两个派生类的定义和实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
protected:
string accountNumber;
string accountName;
double balance;
public:
BankAccount(string accountNumber, string accountName, double balance) {
this->accountNumber = accountNumber;
this->accountName = accountName;
this->balance = balance;
}
virtual void display() {
cout << "Account Number: " << accountNumber << endl;
cout << "Account Name: " << accountName << endl;
cout << "Account Balance: " << balance << endl;
}
virtual bool deposit(double amount) {
balance += amount;
return true;
}
virtual bool withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
return true;
}
else {
return false;
}
}
};
class CheckingAccount : public BankAccount {
private:
double overdraftLimit;
public:
CheckingAccount(string accountNumber, string accountName, double balance, double overdraftLimit)
: BankAccount(accountNumber, accountName, balance) {
this->overdraftLimit = overdraftLimit;
}
void display() {
BankAccount::display();
cout << "Overdraft Limit: " << overdraftLimit << endl;
}
bool withdraw(double amount) {
if (balance + overdraftLimit >= amount) {
balance -= amount;
return true;
}
else {
return false;
}
}
};
class SavingsAccount : public BankAccount {
private:
double interestRate;
public:
SavingsAccount(string accountNumber, string accountName, double balance, double interestRate)
: BankAccount(accountNumber, accountName, balance) {
this->interestRate = interestRate;
}
void display() {
BankAccount::display();
cout << "Interest Rate: " << interestRate << endl;
}
void addInterest() {
balance += balance * interestRate;
}
};
int main() {
BankAccount* accountList[10];
int numAccounts = 0;
int choice;
do {
cout << "Bank Account Management System" << endl;
cout << "1. Open a Checking Account" << endl;
cout << "2. Open a Savings Account" << endl;
cout << "3. Deposit Money" << endl;
cout << "4. Withdraw Money" << endl;
cout << "5. Display Account Information" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
string accountNumber, accountName;
double balance, overdraftLimit;
cout << "Enter Account Number: ";
cin >> accountNumber;
cout << "Enter Account Name: ";
cin >> accountName;
cout << "Enter Opening Balance: ";
cin >> balance;
cout << "Enter Overdraft Limit: ";
cin >> overdraftLimit;
accountList[numAccounts] = new CheckingAccount(accountNumber, accountName, balance, overdraftLimit);
numAccounts++;
break;
case 2:
string accountNumber1, accountName1;
double balance1, interestRate;
cout << "Enter Account Number: ";
cin >> accountNumber1;
cout << "Enter Account Name: ";
cin >> accountName1;
cout << "Enter Opening Balance: ";
cin >> balance1;
cout << "Enter Interest Rate: ";
cin >> interestRate;
accountList[numAccounts] = new SavingsAccount(accountNumber1, accountName1, balance1, interestRate);
numAccounts++;
break;
case 3:
string accountNumber2;
double amount;
cout << "Enter Account Number: ";
cin >> accountNumber2;
cout << "Enter Deposit Amount: ";
cin >> amount;
for (int i = 0; i < numAccounts; i++) {
if (accountList[i]->accountNumber == accountNumber2) {
accountList[i]->deposit(amount);
cout << "Deposit Successful!" << endl;
break;
}
}
break;
case 4:
string accountNumber3;
double amount1;
cout << "Enter Account Number: ";
cin >> accountNumber3;
cout << "Enter Withdrawal Amount: ";
cin >> amount1;
for (int i = 0; i < numAccounts; i++) {
if (accountList[i]->accountNumber == accountNumber3) {
if (accountList[i]->withdraw(amount1)) {
cout << "Withdrawal Successful!" << endl;
}
else {
cout << "Insufficient Balance!" << endl;
}
break;
}
}
break;
case 5:
string accountNumber4;
cout << "Enter Account Number: ";
cin >> accountNumber4;
for (int i = 0; i < numAccounts; i++) {
if (accountList[i]->accountNumber == accountNumber4) {
accountList[i]->display();
break;
}
}
break;
case 6:
cout << "Thank you for using Bank Account Management System!" << endl;
break;
default:
cout << "Invalid Choice!" << endl;
}
} while (choice != 6);
return 0;
}
```
在这个示例中,我们定义了一个包含10个银行账户指针的数组accountList,以及一个变量numAccounts,用于跟踪当前数组中的银行账户数。在主函数中,我们使用一个菜单来允许用户选择不同的操作,例如开户、存款、取款、查询余额等操作。
当用户选择开户操作时,我们会提示用户输入账户号、账户名、开户金额以及特定于账户类型的其他属性,例如overdraftLimit或interestRate。然后,我们将创建一个新的CheckingAccount或SavingsAccount对象,并将其加入到accountList数组中。
当用户选择存款或取款操作时,我们会提示用户输入账户号和金额,并遍历accountList数组以查找匹配的账户。如果找到了匹配的账户,我们将调用其deposit或withdraw方法来执行相应的操作。
当用户选择查询余额操作时,我们会提示用户输入账户号,并遍历accountList数组以查找匹配的账户。如果找到了匹配的账户,我们将调用其display方法来显示账户信息。
当用户选择退出操作时,我们将退出程序。
这就是一个简单的基于多态继承的银行管理系统示例,希望能对您有所帮助。
阅读全文