根据16.12中所定义的Account类、Savings类和Checking类,编写一个应用程序,它读入一系列的账号和存款,创建若干个储蓄和结算账户, 直到碰到一个标志结束的符号,并输出所有账号的存款数据。 要求:(1) 多文件结构体系 (2) 每个类都要用到 (3) 体会基于类的链表结构(4)请用C++语言书写
时间: 2024-02-21 22:57:08 浏览: 128
好的,我会根据您的要求回答。以下是一个基于类的链表结构的C++程序,用于创建储蓄和结算账户,并输出它们的存款数据。
首先,我们需要定义三个类:Account类、Savings类和Checking类。Account类是一个抽象基类,Savings类和Checking类是Account类的派生类。这里我们将这些类定义在不同的头文件中。
Account类的头文件 Account.h:
```cpp
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account {
public:
virtual void deposit(double amount) = 0;
virtual void withdraw(double amount) = 0;
virtual double get_balance() const = 0;
virtual void display() const = 0;
virtual ~Account() {}
};
#endif
```
Savings类的头文件 Savings.h:
```cpp
#ifndef SAVINGS_H
#define SAVINGS_H
#include "Account.h"
class Savings : public Account {
private:
double balance;
public:
Savings(double balance = 0.0);
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual double get_balance() const;
virtual void display() const;
};
#endif
```
Checking类的头文件 Checking.h:
```cpp
#ifndef CHECKING_H
#define CHECKING_H
#include "Account.h"
class Checking : public Account {
private:
double balance;
public:
Checking(double balance = 0.0);
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual double get_balance() const;
virtual void display() const;
};
#endif
```
接下来,我们需要实现这些类。这里我们将它们的实现分别放在 Account.cpp、Savings.cpp 和 Checking.cpp 中。
Account.cpp:
```cpp
#include "Account.h"
```
Savings.cpp:
```cpp
#include "Savings.h"
#include <iostream>
Savings::Savings(double balance) : balance(balance) {}
void Savings::deposit(double amount) {
balance += amount;
}
void Savings::withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
std::cout << "Insufficient funds\n";
}
}
double Savings::get_balance() const {
return balance;
}
void Savings::display() const {
std::cout << "Savings balance: " << balance << std::endl;
}
```
Checking.cpp:
```cpp
#include "Checking.h"
#include <iostream>
Checking::Checking(double balance) : balance(balance) {}
void Checking::deposit(double amount) {
balance += amount;
}
void Checking::withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
std::cout << "Insufficient funds\n";
}
}
double Checking::get_balance() const {
return balance;
}
void Checking::display() const {
std::cout << "Checking balance: " << balance << std::endl;
}
```
现在我们已经定义了这些类,接下来我们可以编写一个应用程序来创建储蓄和结算账户,并输出它们的存款数据。这里我们将应用程序分为三个文件:main.cpp、List.h 和 List.cpp。
main.cpp:
```cpp
#include <iostream>
#include <string>
#include "List.h"
int main() {
List<Account*> accounts;
std::string type;
double balance;
while (true) {
std::cout << "Enter account type (Savings/Checking) and balance, or 'exit' to quit: ";
std::cin >> type;
if (type == "exit") {
break;
}
std::cin >> balance;
if (type == "Savings") {
accounts.push_back(new Savings(balance));
} else if (type == "Checking") {
accounts.push_back(new Checking(balance));
}
}
for (auto& account : accounts) {
account->display();
}
return 0;
}
```
List.h:
```cpp
#ifndef LIST_H
#define LIST_H
template <typename T>
class Node {
public:
T data;
Node<T>* next;
Node<T>* prev;
Node(T data = T()) : data(data), next(nullptr), prev(nullptr) {}
};
template <typename T>
class List {
private:
Node<T>* head;
Node<T>* tail;
int size;
public:
List();
~List();
void push_back(T data);
int get_size() const;
T& operator[](const int index);
};
template <typename T>
List<T>::List() : head(nullptr), tail(nullptr), size(0) {}
template <typename T>
List<T>::~List() {
while (size > 0) {
pop_front();
}
}
template <typename T>
void List<T>::push_back(T data) {
Node<T>* node = new Node<T>(data);
if (size == 0) {
head = node;
tail = node;
} else {
tail->next = node;
node->prev = tail;
tail = node;
}
size++;
}
template <typename T>
int List<T>::get_size() const {
return size;
}
template <typename T>
T& List<T>::operator[](const int index) {
int i = 0;
Node<T>* current = head;
while (current != nullptr) {
if (i == index) {
return current->data;
}
current = current->next;
i++;
}
}
#endif
```
List.cpp:
```cpp
#include "List.h"
template <typename T>
void List<T>::pop_front() {
if (size == 0) {
return;
}
Node<T>* temp = head;
head = head->next;
if (head == nullptr) {
tail = nullptr;
} else {
head->prev = nullptr;
}
delete temp;
size--;
}
```
以上就是一个基于类的链表结构的C++程序,用于创建储蓄和结算账户,并输出它们的存款数据。
阅读全文