这是我到目前为止的代码,它可以编译并运行良好,但我需要帮助来调整它。这是一款银行应用程序,目前仅适用于一个帐户。
它需要适应两个新文件:bank.h 和 bank.cpp,并且 main 应该包含一个指向 bank 的指针,而 bank 应该包含一个指向 account 实例的指针数组。
所以新界面的工作方式类似于:
帐户> 1 12
1 是账户#,12 是存入金额。
我真的需要帮助来调整我的代码来做到这一点,我不知道如何在银行中创建指向帐户实例的指针数组。任何帮助深表感谢。
//main.cpp file
using namespace std;
#include <iostream>
#include <stdlib.h>
#include "account.h"
//allocate new space for class pointer
account* c = new account;
//function for handling I/O
int accounting(){
string command;
cout << "account> ";
cin >> command;
//exits prompt
if (command == "quit"){
exit(0);
}
//overwrites account balance
else if (command == "init"){
cin >> c->value;
c->init();
accounting();
}
//prints balance
else if (command == "balance"){
cout << "" << c->account_balance() << endl;
accounting();
}
//deposits value
else if (command == "deposit"){
cin >> c->value;
c->deposit();
accounting();
}
//withdraws value
else if (command == "withdraw"){
cin >> c->value;
c->withdraw();
accounting();
}
//error handling
else{
cout << "Error! Invalid operator." << endl;
accounting();
}
//frees memory
delete c;
}
int main() {
accounting();
return 0;
}
//account.h 头文件,包含具有共享变量和函数的类
class account{
private:
int balance;
public:
account();
~account();
int value;
int account_balance();
int deposit();
int withdraw();
int init();
};
//account.cpp 实现文件
using namespace std;
#include <iostream>
#include "account.h"
account::account(){
}
account::~account(){
}
//balance overwrite function
int account::init(){
balance = value;
}
//balance function
int account::account_balance() {
return balance;
}
//deposit function
int account::deposit(){
balance += value;
}
//withdraw function
int account::withdraw(){
//error handling
if(value>balance){
cout << "Error! insufficient funds." << endl;
return 0;
}
balance -= value;
}