2017-12-05 36 views
0

親クラスAccountと派生クラスBankAccountを含むプログラムを作成しています。当初は、残高を5000に設定する必要があります。特定の取引が行われると、この残高が更新されます。また、プログラムが終了するたびに、プログラムは最新の残高を返す必要があります。以下は私のコードです。しかし、コンストラクタを適切に初期化することができないため、初期バランスが正しく設定されることはありません。私が間違っていることを教えてください。これは、または単に「初期化」「コンストラクタでメンバ変数を初期化」と呼ばれているコンストラクタを初期化できません

class Account 
{ 
public: 


    void setCashBal(); //updates the cash balance in account whenever a transaction is made 
    double getCashBal(); //retrieves recent cash balance 


protected: 
    double cash_balance; 
}; 


void Account::setCashBal() 
{ 

    double initial_bal; 


    ifstream read_cash_file("cash_bal_file.txt", ios_base::in); 

    int count = 0; 

    if (read_cash_file.is_open()) //check whether the file could be openend 
    { 

     while (!(read_cash_file.eof())) //reading until the end of file 
     { 
      count = count + 1; 
      break; 
     } 
     read_cash_file.close(); 

     if (count == 0)//if the file is opened the first time, initial cash balance is 5000 
     { 
      initial_bal = 5000; 

      ofstream write_cash_file("cash_bal_file.txt", ios::out); 


      write_cash_file << initial_bal; //writing the initial value of cash balance in cash_bal_file 
      write_cash_file.close(); 

      read_cash_file.open("cash_bal_file.txt", ios_base::in); 
      read_cash_file >> cash_balance; 
      read_cash_file.close(); 
     } 


     else //getting the latest cash balance 
     { 
      read_cash_file.open("cash_bal_file.txt", ios::in); 
      read_cash_file >> cash_balance; 
      read_cash_file.close(); 
     } 
    } 

    else //case when the file could not be openend 
     cout << endl << "Sorry, cash_bal_file could not be opened." << endl; 
} 

double Account::getCashBal() 
{ 
    return cash_balance; 
} 


    class BankAccount :public Account 
    { 
    public: 
     BankAccount(); 
     void viewBalance(); 
    }; 

    BankAccount::BankAccount() 

    { 
     setCashBal(); 
     cash_balance = getCashBal(); 
    } 


    void BankAccount::viewBalance() 
    { 

     double balance; 
     ifstream view_bal("cash_bal_file.txt", ios_base::in); //getting the latest cash_balance from cash_bal_file. 
     view_bal >> balance; 


     cout << endl << "The balance in your bank account is " << balance << endl; 
    } 


    int main() 
    { 
     BankAccount bnkObj; 
     bnkObj.viewBalance(); 

    return 0; 
    } 
+0

!.eof()を使用しないでください。本当に悪いです –

+0

string name; while(read_cash_file >> name){}はどうしますか?この場合、 –

+0

は 'string name; getline(read_cash_file、name); '。またコンストラクタが必要な場合は0をデフォルトにします。 –

答えて

0
class Account 
{ 
    public: 
    int n; 

    Account(): n(5000) 
    {} 
}; 

。初心者用のC++テキストで "initialize"を検索すると、次のようなものが見つかります。

+0

私はこれをしました。しかし、プログラムが実行されるたびに、残高は5000に設定されます。プログラムは、特定の預金または引き出し後に最新の残高を返す必要があります。 –

+0

@RestingPlatypus:[rubs temples]これは、あなたが助けを求めたものである変数を初期化するコンストラクタを得る方法です。あなたの課題がこれに加えて他のことを求めている場合は、あなた自身でコードを書く必要があります。または[プログラマーを雇う](https://stackoverflow.com/jobs)。 – Beta

+1

"プログラムが実行されるたびに" ...あなたのプログラムは '永続的ストア'を実行し、後で再ロードするために値をファイルに保存する必要があることを示唆しています。プログラムを再起動するたびに、まず永続ストアからロードし、その番号を使用してデータを初期化します。積み込みと預金の間に、お客様が要求した行動(預金、引き出し、残高など)を実行します。 –

関連する問題