根据16.12中所定义的Account类、Savings类和Checking类,编写一个应用程序,它读入一系列的账号和存款,创建若干个储蓄和结算账户, 直到碰到一个标志结束的符号,并输出所有账号的存款数据。 要求:(1) 多文件结构体系 (2) 每个类都要用到 (3) 体会基于类的链表结构
时间: 2024-02-21 07:57:57 浏览: 69
微信小程序常见FAQ(16.12.12-16.12.18)
好的,我会根据您的要求回答这个问题。根据您的要求,我们需要编写一个多文件的程序,其中包含Account类、Savings类和Checking类。我们还需要使用基于类的链表结构来管理这些账户。以下是一个可能的解决方案:
首先,我们需要创建一个账户类 Account。在 Account.h 文件中,我们定义了 Account 类的基本结构和函数:
```cpp
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account {
public:
Account(int accountNumber, double balance);
virtual ~Account();
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual double getBalance() const;
virtual void print() const;
protected:
int accountNumber;
double balance;
};
#endif
```
在 Account.cpp 文件中,我们实现了 Account 类的函数:
```cpp
#include "Account.h"
#include <iostream>
Account::Account(int accountNumber, double balance) :
accountNumber(accountNumber), balance(balance) {}
Account::~Account() {}
void Account::deposit(double amount) {
balance += amount;
}
void Account::withdraw(double amount) {
balance -= amount;
}
double Account::getBalance() const {
return balance;
}
void Account::print() const {
std::cout << "Account #" << accountNumber << " has balance $" << balance << std::endl;
}
```
接下来,我们需要创建一个储蓄账户类 Savings。在 Savings.h 文件中,我们定义了 Savings 类的基本结构和函数:
```cpp
#ifndef SAVINGS_H
#define SAVINGS_H
#include "Account.h"
class Savings : public Account {
public:
Savings(int accountNumber, double balance, double interestRate);
virtual ~Savings();
virtual void update();
protected:
double interestRate;
};
#endif
```
在 Savings.cpp 文件中,我们实现了 Savings 类的函数:
```cpp
#include "Savings.h"
Savings::Savings(int accountNumber, double balance, double interestRate) :
Account(accountNumber, balance), interestRate(interestRate) {}
Savings::~Savings() {}
void Savings::update() {
balance *= (1 + interestRate);
}
```
最后,我们需要创建一个结算账户类 Checking。在 Checking.h 文件中,我们定义了 Checking 类的基本结构和函数:
```cpp
#ifndef CHECKING_H
#define CHECKING_H
#include "Account.h"
class Checking : public Account {
public:
Checking(int accountNumber, double balance, double fee);
virtual ~Checking();
virtual void withdraw(double amount);
protected:
double fee;
};
#endif
```
在 Checking.cpp 文件中,我们实现了 Checking 类的函数:
```cpp
#include "Checking.h"
Checking::Checking(int accountNumber, double balance, double fee) :
Account(accountNumber, balance), fee(fee) {}
Checking::~Checking() {}
void Checking::withdraw(double amount) {
balance -= amount + fee;
}
```
现在我们已经定义了三个类,我们需要编写一个主程序来测试这些类。我们可以使用一个基于类的链表来管理这些账户。在 main.cpp 文件中,我们可以编写以下代码:
```cpp
#include "Account.h"
#include "Savings.h"
#include "Checking.h"
#include <iostream>
#include <string>
using namespace std;
class Node {
public:
Account* account;
Node* next;
Node(Account* account) : account(account), next(nullptr) {}
~Node() {
delete account;
delete next;
}
};
void printAccounts(Node* head) {
Node* p = head;
while (p != nullptr) {
p->account->print();
p = p->next;
}
}
int main() {
Node* head = nullptr;
string command;
while (true) {
cout << "Enter account type (Savings/Checking) and account number (or q to quit): ";
cin >> command;
if (command == "q") break;
int accountNumber;
cin >> accountNumber;
if (command == "Savings") {
double balance, interestRate;
cout << "Enter balance and interest rate: ";
cin >> balance >> interestRate;
Savings* s = new Savings(accountNumber, balance, interestRate);
if (head == nullptr) {
head = new Node(s);
} else {
Node* p = head;
while (p->next != nullptr) p = p->next;
p->next = new Node(s);
}
} else if (command == "Checking") {
double balance, fee;
cout << "Enter balance and fee: ";
cin >> balance >> fee;
Checking* c = new Checking(accountNumber, balance, fee);
if (head == nullptr) {
head = new Node(c);
} else {
Node* p = head;
while (p->next != nullptr) p = p->next;
p->next = new Node(c);
}
} else {
cout << "Unknown command!" << endl;
}
}
printAccounts(head);
delete head;
return 0;
}
```
这个程序会不断读入账户类型和账户号码,然后根据账户类型创建一个储蓄账户或结算账户,并将其添加到链表中。当用户输入 "q" 时,程序会停止读入,然后输出所有账户的存款数据。
这就是一个可能的解决方案,希望能对您有所帮助!
阅读全文