指向类实例的指针数组

Array of pointers to class instance c++

本文关键字:指针 数组 实例      更新时间:2023-10-16

这是我到目前为止的代码,它编译和运行良好,但我需要帮助适应它。这是一个银行应用程序,目前只支持一个账户。

它需要适应两个新文件:bank.h和bank.cpp,并且main应该包含一个指向bank的指针,而bank应该包含一个指向account实例的指针数组。

所以新的界面会像这样工作:

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;
}

首先,

//error handling    
else{
    cout << "Error! Invalid operator." << endl;
    accounting();
    }

这看起来很丑,你在每次错误的输入后都递归地调用会计函数。想象一下这样一种情况:用户输入了1 000 000次错误的输入……然后,您将尝试释放内存1 000 000次-在一次成功输入之后!

//frees memory          
delete c;   

整个会计功能设计错误。我想你不会想在某笔交易后销毁你的账户吧?我认为一个人从他的1000万美元的账户中取出10美元,这个账户将会被销毁,他会立即换银行:)

所以带有continue的while循环可以是解

//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;

不是类成员,它应该是方法withdraw和deposit的参数,如下所示

//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;

}

对于数组,可以使用std::vector类。

std::vector<account *>MyAccounts;
MyAccounts.push_back(new account());

那么你可以像使用数组一样使用它来访问它。

MyAccounts[i]->accountFunction();

我不太了解你的代码,所以我在这里只给出一些一般的例子。

在您的银行类中,您有一个成员(如上所示)MyAccounts。现在,当您向银行添加新帐户时,您可以使用推回功能。

例如,添加一个新帐户,并设置100个货币项目的初始金额。

 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;
}

更新:这只是一个Bank类,帐户向量作为私有成员变量。AddAccount是一个公共函数,可以将account添加到vector