类实例C++的指针数组
这是我到目前为止的代码,它编译和运行良好,但我需要帮助调整它。这是一个银行应用程序,目前只适用于一个帐户 它需要适应两个新文件:bank.h和bank.cpp,main应该包含指向bank的指针,bank应该包含指向account实例的指针数组 因此,新界面的工作原理如下: 帐户>1 12 1是账户,12是存款金额 我真的需要帮助修改代码来实现这一点,我不知道如何在银行中创建指向account实例的指针数组。非常感谢您的帮助类实例C++的指针数组,c++,arrays,class,pointers,implementation,C++,Arrays,Class,Pointers,Implementation,这是我到目前为止的代码,它编译和运行良好,但我需要帮助调整它。这是一个银行应用程序,目前只适用于一个帐户 它需要适应两个新文件:bank.h和bank.cpp,main应该包含指向bank的指针,bank应该包含指向account实例的指针数组 因此,新界面的工作原理如下: 帐户>1 12 1是账户,12是存款金额 我真的需要帮助修改代码来实现这一点,我不知道如何在银行中创建指向account实例的指针数组。非常感谢您的帮助 //main.cpp file using namespace st
//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.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;
}
对于数组,可以使用std::vector类
std::vector<account *>MyAccounts;
MyAccounts.push_back(new account());
更新
我对你的代码了解得不够,所以这里我只举一些一般的例子
在您的银行类中,您有一个成员,如上面MyAccounts所示。现在,当您向银行添加新帐户时,您可以使用推送功能进行操作
例如,添加新帐户并设置100个moneyitems的初始金额
MyAccounts.push_back(new account());
size_t i = MyAccounts.size();
MyAccounts[i]->setAmount(100);
你可以像下面这样做
class Bank
{
public:
int AddAccount(Account act){ m_vecAccts.push_back(act);}
....
private:
...
std:vector<account> m_vecAccts;
}
更新:
这只是一个银行类,帐户向量作为私有成员变量。AddAccount是一个公共函数,它可以将account添加到vector中
//error handling
else{
cout << "Error! Invalid operator." << endl;
accounting();
}
整个会计功能设计错误。我想你不想在某种交易后毁掉这个账户,对吧?我认为从1000万美元账户中提取10美元的人会立即更换银行,该账户将被销毁:
因此,一段时间的持续循环可能是解决方案
//function for handling I/O
int accounting(){
string command;
while(true) {
cout << "account> ";
cin >> command;
//exits prompt
if (command == "quit"){
break;
}
//overwrites account balance
else if (command == "init"){
cin >> c->value;
c->init();
continue;
}
//prints balance
else if (command == "balance"){
cout << "" << c->account_balance() << endl;
continue;
}
//deposits value
else if (command == "deposit"){
cin >> c->value;
c->deposit();
accounting();
}
//withdraws value
else if (command == "withdraw"){
cin >> c->value;
c->withdraw();
continue;
}
//error handling
else{
cout << "Error! Invalid operator." << endl;
continue;
}
}
不是一个类成员,它应该是取款和存款方法的参数,如下所示
//deposit function
void account::deposit(int value){ //int changed to void, you are not returning anything!
balance += value;
}
//withdraw function
bool account::withdraw(int value){
//error handling
if(value>balance){
cout << "Error! insufficient funds." << endl;
return false;
}
if(value<0) {
cout << "Haha, nice try!" << endl;
return false;
}
balance -= value;
return true;
}这似乎超出了我的理解力,你能解释更多吗?@lanschramm你能解释一下你的理解力吗。你能帮我在bank.h和bank.cppI中实现这一点吗?我在这里向你展示了一些示例代码。如果你有多个帐户,你需要一些方法来识别它。要么给它一个名字,要么给它一个id,比如银行账号。因此,当你想存款时,你必须指定使用正确帐户的id。对不起,我应该说得更清楚一些。它应该有一个由20个索引组成的数组,每个索引都是一个帐户。因此,帐户[0]是第一个帐户。在这种情况下,您可以执行std::vectoryAccounts20来为20个帐户保留足够的空间。或者,如果您总是想拥有这20个acocont,您可以去掉指针std::vectoryAccounts 20,这将使代码更容易。将始终有20个帐户,因此我只想创建一个20个的数组,并为每个索引分配一个值。那么我如何在bank.cpp和类中使用std::vector呢?对不起,我问了这么多问题,但我对C++很陌生。
//frees memory
delete c;
//function for handling I/O
int accounting(){
string command;
while(true) {
cout << "account> ";
cin >> command;
//exits prompt
if (command == "quit"){
break;
}
//overwrites account balance
else if (command == "init"){
cin >> c->value;
c->init();
continue;
}
//prints balance
else if (command == "balance"){
cout << "" << c->account_balance() << endl;
continue;
}
//deposits value
else if (command == "deposit"){
cin >> c->value;
c->deposit();
accounting();
}
//withdraws value
else if (command == "withdraw"){
cin >> c->value;
c->withdraw();
continue;
}
//error handling
else{
cout << "Error! Invalid operator." << endl;
continue;
}
}
int value;
//deposit function
void account::deposit(int value){ //int changed to void, you are not returning anything!
balance += value;
}
//withdraw function
bool account::withdraw(int value){
//error handling
if(value>balance){
cout << "Error! insufficient funds." << endl;
return false;
}
if(value<0) {
cout << "Haha, nice try!" << endl;
return false;
}
balance -= value;
return true;