是,并且使用c++
时间: 2024-02-16 13:03:15 浏览: 23
好的,下面是一个基于类的C++程序,用于读取一系列的账户和存款,创建储蓄和结算账户,直到遇到一个标志符号为止,然后输出所有账户的存款数据。程序使用多文件结构体系和基于类的链表结构。
首先,我们需要创建三个类:Account、Savings和Checking。 Account类是基类,而Savings和Checking类是派生类。下面是这些类的头文件:
Account.h
```c++
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account {
public:
Account();
Account(double balance);
virtual ~Account();
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual double get_balance() const;
private:
double balance;
};
#endif
```
Savings.h
```c++
#ifndef SAVINGS_H
#define SAVINGS_H
#include "Account.h"
class Savings : public Account {
public:
Savings();
Savings(double balance, double interest_rate);
virtual ~Savings();
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual void add_interest();
virtual double get_interest_rate() const;
private:
double interest_rate;
};
#endif
```
Checking.h
```c++
#ifndef CHECKING_H
#define CHECKING_H
#include "Account.h"
class Checking : public Account {
public:
Checking();
Checking(double balance, double fee);
virtual ~Checking();
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual void deduct_fees();
virtual double get_fee() const;
private:
double fee;
};
#endif
```
然后,我们需要创建一个链表节点类,用于存储每个账户的信息。下面是节点类的头文件:
Node.h
```c++
#ifndef NODE_H
#define NODE_H
#include "Account.h"
class Node {
public:
Node(Account* account);
virtual ~Node();
Account* get_account() const;
Node* get_next() const;
void set_next(Node* next);
private:
Account* account;
Node* next;
};
#endif
```
接下来是主程序。它将读取一系列的账号和存款,创建储蓄和结算账户,并将每个账户存储在一个链表中。当遇到一个特定的标志符号时,程序将输出所有账户的存款数据。下面是主程序的代码:
main.cpp
```c++
#include <iostream>
#include <string>
#include "Account.h"
#include "Savings.h"
#include "Checking.h"
#include "Node.h"
using namespace std;
const string FLAG = "exit";
void print_accounts(Node* head) {
cout << "All accounts:" << endl;
Node* current = head;
while (current != nullptr) {
cout << "Balance: " << current->get_account()->get_balance() << endl;
current = current->get_next();
}
}
int main() {
Node* head = nullptr;
Node* tail = nullptr;
while (true) {
string input;
double balance = 0;
double interest_rate = 0;
double fee = 0;
cout << "Enter account type (savings/checking) and balance, separated by a space: ";
cin >> input;
if (input == FLAG) {
break;
}
cin >> balance;
if (input == "savings") {
cout << "Enter interest rate: ";
cin >> interest_rate;
Savings* savings = new Savings(balance, interest_rate);
if (head == nullptr) {
head = new Node(savings);
tail = head;
} else {
Node* node = new Node(savings);
tail->set_next(node);
tail = node;
}
} else if (input == "checking") {
cout << "Enter fee: ";
cin >> fee;
Checking* checking = new Checking(balance, fee);
if (head == nullptr) {
head = new Node(checking);
tail = head;
} else {
Node* node = new Node(checking);
tail->set_next(node);
tail = node;
}
} else {
cout << "Invalid account type" << endl;
}
}
print_accounts(head);
Node* current = head;
while (current != nullptr) {
Node* next = current->get_next();
delete current;
current = next;
}
return 0;
}
```
最后,我们需要实现节点类、基类和派生类的方法。这些方法可以在其各自的源文件中实现。
Node.cpp
```c++
#include "Node.h"
Node::Node(Account* account) {
this->account = account;
this->next = nullptr;
}
Node::~Node() {
delete account;
}
Account* Node::get_account() const {
return account;
}
Node* Node::get_next() const {
return next;
}
void Node::set_next(Node* next) {
this->next = next;
}
```
Account.cpp
```c++
#include "Account.h"
Account::Account() {
this->balance = 0;
}
Account::Account(double balance) {
this->balance = balance;
}
Account::~Account() {}
void Account::deposit(double amount) {
balance += amount;
}
void Account::withdraw(double amount) {
balance -= amount;
}
double Account::get_balance() const {
return balance;
}
```
Savings.cpp
```c++
#include "Savings.h"
Savings::Savings() {
this->interest_rate = 0;
}
Savings::Savings(double balance, double interest_rate) : Account(balance) {
this->interest_rate = interest_rate;
}
Savings::~Savings() {}
void Savings::deposit(double amount) {
Account::deposit(amount);
add_interest();
}
void Savings::withdraw(double amount) {
Account::withdraw(amount);
add_interest();
}
void Savings::add_interest() {
double interest = get_balance() * interest_rate;
deposit(interest);
}
double Savings::get_interest_rate() const {
return interest_rate;
}
```
Checking.cpp
```c++
#include "Checking.h"
Checking::Checking() {
this->fee = 0;
}
Checking::Checking(double balance, double fee) : Account(balance) {
this->fee = fee;
}
Checking::~Checking() {}
void Checking::deposit(double amount) {
Account::deposit(amount);
deduct_fees();
}
void Checking::withdraw(double amount) {
Account::withdraw(amount);
deduct_fees();
}
void Checking::deduct_fees() {
double fees = fee;
withdraw(fees);
}
double Checking::get_fee() const {
return fee;
}
```
这就是一个简单的基于类的C++程序,用于读取一系列账户和存款,创建储蓄和结算账户,并将每个账户存储在一个链表中。当遇到一个特定的标志符号时,程序将输出所有账户的存款数据。程序使用多文件结构体系和基于类的链表结构。