【问题标题】:Array of pointers to class instance c++指向类实例c ++的指针数组
【发布时间】:2013-06-01 15:00:26
【问题描述】:

这是我目前拥有的代码,它可以编译并运行良好,但我需要帮助来调整它。这是一款目前仅适用于一个帐户的银行应用程序。

它需要适应两个新文件: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;
}

【问题讨论】:

    标签: c++ arrays class pointers implementation


    【解决方案1】:

    首先,

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

    不是类成员,应该是提款和提款方法的参数,像这样

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

    }

    【讨论】:

      【解决方案2】:

      对于数组,您可以使用 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);
      

      【讨论】:

      • 你能帮我在bank.h和bank.cpp中实现这个
      • 我在这里向您展示了一些示例代码。如果您有多个帐户,则需要某种方法来识别它。给它一个名字或一个 ID,比如银行帐号。所以当你想存钱时,你必须指定 id 才能使用正确的帐户。
      • 对不起,我应该更清楚。它应该有一个包含 20 个索引的数组,每个索引都是一个帐户。所以 account[0] 是第一个帐号。
      • 在这种情况下,您可以使用std::vector&lt;account *&gt;MyAccounts(20) 为 20 个帐户预留足够的空间。或者如果你总是想拥有这 20 个帐户,你可以去掉指针 std::vector&lt;account&gt;MyAccounts(20),这将使代码更容易。
      • 总会有 20 个帐户,所以我想创建一个 20 的数组并为每个索引分配一个值。那么我将如何在 bank.cpp 和类中使用 std::vector 呢?很抱歉我问了这么多问题,但我是 C++ 新手。
      【解决方案3】:

      您可以执行以下操作

         class Bank
      {
      public:
      int AddAccount(Account act){ m_vecAccts.push_back(act);}
      ....
      private:
      ...
      std:vector<account> m_vecAccts;
      }
      

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

      【讨论】:

      • 这似乎是我的头,你能解释更多吗?
      • @lanschramm 你能解释一下你脑子里发生了什么吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      相关资源
      最近更新 更多