如何在C ++中显示来自另一个类的数据

时间:2013-06-25 16:59:40

标签: c++

我在类中有一个方法,通过询问文件中的用户帐户输入数字并实例化类Users的对象。它会立即检查哪个帐户(当前,储蓄)并取决于实例化某个类。代码如下所示:

class Bank
{
public:
    Bank();
    enum AccType{
        Current = 'T',
        Savings = 'Z',
        Exchange = 'D'
         };
    void registerUser(void);
    void createNewUser(void);
    void deleteUser(void);
};

void Bank::registerUser(void)
{
    filePathBuilder dataloader;
    string fname = dataloader.filePath("account.dat");
    Users *us;
    double saldo=0.0;
    char data[45];
    string acc_number="";
    string vlasnik,prezime,adresa,amount;
    char tip='\0';
    string brojRac;
    string line;
    bool isTrue = false;

    ifstream fin;
    fin.open(fname);
    if(!fin){
        cout<<"Error - could not open a file. . .\n"<<endl;
        exit(1);
    }

    vector<Users> korisnici;
    cout<< "Account number: "<<endl;
    cin>>brojRac;

    while(fin.good()){

        fin>>data;
        if(data == brojRac){
            acc_number = data; 
            fin>>data;
            vlasnik = data;
            fin>>data;
            prezime = data;
            fin>>data;
            adresa = data;
            fin>>data;
            tip = data[0];
            fin>>data;
            amount = data;
            saldo = atof(amount.c_str());
            break;
        }
    }

    fin.close();
    if(acc_number == ""){
        cout<<"Don't exists"<<endl;
        exit(0);
    }
    us = new Users;
    us->setName(vlasnik);
    us->setSurname(prezime);
    us->setAddress(adresa);
    enum AccType tipRac = static_cast<AccType>(tip);
    if(tipRac == 'T')
    {

    }
    else if(tipRac == 'Z')
    {
        ZiroAccount zacc;
        long acc = atoi(acc_number.c_str());
        zacc.setAccNumber(acc);
        zacc.setBalance(saldo);
        zacc.setAccType(tip);
        us->setAccount(zacc);
    }
    else{

    }

    korisnici.push_back(*us);

    vector<Users>::iterator it;

    for(it=korisnici.begin(); it != korisnici.end(); ++it){
        it->printUsersInfo();
        it->printUsersInfo();

    }

    isTrue = true;
}

我在另一个显示主菜单的类中有一个方法。

void Initialize::main_menu(void)
{
    bool flag = false;
    char choice;
    while(flag == false)
    {
        //system("clear");
        cout<<"\n\n\t\t Glavni izbornik"<<endl;
        cout<<"\n\t Odaberite opciju:"<<endl;
        cout<<"\n\t 01 - DISPLAY ACCOUNT"<<endl;
        cout<<"\n\t 02 - DEPOSIT AMOUNT"<<endl;
        cout<<"\n\t 03 - WITHDRAW AMOUNT"<<endl;
        cout<<"\n\t 04 - CHECK BALANCE"<<endl;
        cout<<"\n\t 05 - CLOSE AN ACCOUNT"<<endl;
        cout<<"\n\t 06 - EXIT"<<endl;
        cout<<"\n\n\tYout choice (1-6)";

        cin>>choice;
        switch(choice){

        case '1':
            //HOW TO DISPLAY USERS INFO AND ACCOUNT INFO???
            break;
        case '2':
            //. . . .
            break;
        case '3':
            //. . . .
            break;
        case '4':
            //. . . . 
            break;
        case '5':
            //. . . .
        case '6':
            flag = true;
            cout<<"Exit"<<endl;
            break;
        default:
            cout<<"Wrong input"<<endl;
            cin.get();
        }
    }

}

我的问题是:如果用户从菜单中输入1,如何显示用户和帐户信息?

1 个答案:

答案 0 :(得分:1)

当Bank :: registerUser函数返回它收集的所有数据时都会被销毁。那是因为您创建了用户向量作为局部变量。并且您创建了我们=具有本地变量us的新用户。您需要了解存储生命周期。这些变量可能需要是成员变量,而不是局部变量,因此它们将在函数返回时存活。

您在哪里创建了Bank对象? Initialize :: main_menu函数需要访问Bank对象,以便它可以访问该对象的成员变量。