写一个c++程序 基本账户类型包括:普通活期账户、定期存款账户、理财账户、信用卡账户等,通过查找资料了解各种账户的基本属性,通过程序对外接口,能够对各种账户的基本属性进行配置; 2、 具有基本的账户操作功
时间: 2023-06-14 08:05:24 浏览: 264
银行账户C++的编写
能,如存款、取款、查询余额、查询交易记录等;3、支持多用户管理,每个用户可以拥有多个账户;4、能够进行简单的账户之间的转账操作。
以下是一个基本的实现示例:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 定义账户类
class Account {
public:
Account(string accountType, double balance): accountType(accountType), balance(balance) {
// 初始化账户类型和余额
}
double getBalance() {
return balance;
}
virtual void deposit(double amount) {
balance += amount;
}
virtual void withdraw(double amount) {
balance -= amount;
}
virtual void display() {
cout << "Account Type: " << accountType << endl;
cout << "Balance: " << balance << endl;
}
protected:
string accountType; // 账户类型
double balance; // 余额
};
// 普通活期账户类
class SavingsAccount : public Account {
public:
SavingsAccount(double balance): Account("Savings Account", balance) {
// 初始化账户类型和余额
}
};
// 定期存款账户类
class FixedDepositAccount : public Account {
public:
FixedDepositAccount(double balance, int term): Account("Fixed Deposit Account", balance), term(term) {
// 初始化账户类型、余额和期限
}
virtual void display() {
cout << "Account Type: " << accountType << endl;
cout << "Balance: " << balance << endl;
cout << "Term: " << term << " months" << endl;
}
private:
int term; // 存款期限(月数)
};
// 理财账户类
class InvestmentAccount : public Account {
public:
InvestmentAccount(double balance, double rate): Account("Investment Account", balance), rate(rate) {
// 初始化账户类型、余额和利率
}
virtual void display() {
cout << "Account Type: " << accountType << endl;
cout << "Balance: " << balance << endl;
cout << "Interest Rate: " << rate << "%" << endl;
}
private:
double rate; // 年利率
};
// 信用卡账户类
class CreditCardAccount : public Account {
public:
CreditCardAccount(double balance, double limit): Account("Credit Card Account", balance), limit(limit) {
// 初始化账户类型、余额和信用额度
}
virtual void display() {
cout << "Account Type: " << accountType << endl;
cout << "Balance: " << balance << endl;
cout << "Credit Limit: " << limit << endl;
}
private:
double limit; // 信用额度
};
// 用户类
class User {
public:
User(string name): name(name) {
// 初始化用户名
}
void addAccount(Account* account) {
accounts.push_back(account);
}
void deposit(int accountIndex, double amount) {
accounts[accountIndex]->deposit(amount);
}
void withdraw(int accountIndex, double amount) {
accounts[accountIndex]->withdraw(amount);
}
void displayAccounts() {
cout << "User: " << name << endl;
for (int i = 0; i < accounts.size(); i++) {
cout << "Account " << i + 1 << ":" << endl;
accounts[i]->display();
cout << endl;
}
}
void transfer(int fromAccountIndex, User& toUser, int toAccountIndex, double amount) {
accounts[fromAccountIndex]->withdraw(amount);
toUser.deposit(toAccountIndex, amount);
}
private:
string name; // 用户名
vector<Account*> accounts; // 账户列表
};
int main() {
// 创建用户1
User user1("Alice");
// 创建普通活期账户
SavingsAccount* savingsAccount = new SavingsAccount(1000);
user1.addAccount(savingsAccount);
// 创建定期存款账户
FixedDepositAccount* fixedDepositAccount = new FixedDepositAccount(5000, 12);
user1.addAccount(fixedDepositAccount);
// 创建理财账户
InvestmentAccount* investmentAccount = new InvestmentAccount(20000, 3.5);
user1.addAccount(investmentAccount);
// 创建信用卡账户
CreditCardAccount* creditCardAccount = new CreditCardAccount(0, 5000);
user1.addAccount(creditCardAccount);
// 存款
user1.deposit(0, 500);
user1.deposit(1, 1000);
user1.deposit(2, 500);
// 取款
user1.withdraw(0, 100);
user1.withdraw(3, 200);
// 查询余额和交易记录
user1.displayAccounts();
// 创建用户2
User user2("Bob");
// 创建普通活期账户
SavingsAccount* savingsAccount2 = new SavingsAccount(2000);
user2.addAccount(savingsAccount2);
// 转账
user1.transfer(2, user2, 0, 1000);
// 查询余额和交易记录
user1.displayAccounts();
user2.displayAccounts();
return 0;
}
```
该程序实现了基本的账户管理功能,可以通过创建不同类型的账户来满足不同的需求,并且支持多用户管理和账户之间的转账操作。
阅读全文