我正在使用基類「bankAccount」和兩個派生類「checkingAccount」和「savingsAccount」進行分配。我目前對我所得到的輸出感到困惑。所有期末餘額均爲負數。任何人都可以看看我的代碼,看看他們是否知道爲什麼這可能是?我認爲我在派生類「checkingAccount」的過程函數中做了錯誤的事情。 「savingsAccount」過程函數將會類似我剛剛還沒有做出來,但是導致第一個不工作。謝謝!類和派生類處理幫助
頭:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <iostream>
#include <fstream>
using namespace std;
class bankAccount
{
public:
bankAccount();
void setAccountInfo(int accountNumTemp, double balanceTemp);
void prePrint(char accountType);
void process(char accountType, char transactionTypeTemp, int amountTemp, int j);
void postPrint();
private:
int accountNumber;
double balance;
};
class checkingAccount: public bankAccount
{
public:
void prePrint(int accountNumber, char accountType, double checkingBalance);
checkingAccount();
double checkingAccount:: process(char transactionTypeTemp, int amountTemp, int j, double checkingBalance);
/*applyTansaction();
applyInterest();*/
private:
float interestRate;
int minimumBalance;
float serviceCharge;
};
class savingsAccount: public bankAccount
{
public:
void prePrint(int savingsAccountNumber, char accountType, double savingsBalance);
savingsAccount();
/* applyTansaction();
applyInterest();*/
private:
float interestRate;
};
#endif // HEADER_H_INCLUDED
類實現:
#include "header.h"
bankAccount:: bankAccount()
{
accountNumber = 0;
balance = 0;
}
void bankAccount:: setAccountInfo(int accountNumTemp, double balanceTemp)
{
accountNumber = accountNumTemp;
balance = balanceTemp;
}
void bankAccount:: prePrint(char accountType)
{
if(accountType == 'C')
{
int checkingAccountNumber = accountNumber;
double checkingBalance = balance;
checkingAccount ca;
ca.prePrint(checkingAccountNumber, accountType, checkingBalance);
}
else if (accountType == 'S')
{
int savingsAccountNumber = accountNumber;
double savingsBalance = balance;
savingsAccount sa;
sa.prePrint(savingsAccountNumber, accountType, savingsBalance);
}
}
void bankAccount:: process(char accountType, char transactionTypeTemp, int amountTemp, int j)
{
double checkingBalance;
checkingAccount ca;
//savingsAccount sa;
if (accountType == 'C')
{
checkingBalance = balance;
balance = ca.process(transactionTypeTemp, amountTemp, j, checkingBalance);
}
/*else if (accountType == 'S')
{
savingsBalance = balance;
sa.process(transactionTypeTemp, amountTemp, j, savingsBalance)
}*/
}
void bankAccount:: postPrint()
{
cout << "Balance after processing: " << balance << endl;
}
checkingAccount:: checkingAccount()
{
interestRate = .02;
minimumBalance = 500;
serviceCharge = 20;
}
void checkingAccount:: prePrint(int checkingAccountNumber, char accountType, double checkingBalance)
{
cout << "Account Number:" << checkingAccountNumber << " account type:" << accountType << " Starting Balance:" << checkingBalance << endl;
}
double checkingAccount:: process(char transactionTypeTemp, int amountTemp, int j, double checkingBalance)
{
if (transactionTypeTemp == 'D')
{
checkingBalance = checkingBalance + amountTemp;
checkingBalance = (checkingBalance * interestRate);
}
else if (transactionTypeTemp == 'W')
{
if ((checkingBalance = checkingBalance - amountTemp) < 0)
{
cout << "error: transaction number" << j + 1 << " never occured due to insufficent funds." << endl;
}
else
{
checkingBalance = checkingBalance - amountTemp;
if(checkingBalance < minimumBalance) //if last transaction brought the balance below minimum balance
{
checkingBalance = (checkingBalance - serviceCharge); //apply service charge
checkingBalance = (checkingBalance * interestRate); //apply interest
}
else // if last transaction did not bring the balance below minimum balance
{
checkingBalance = (checkingBalance * interestRate); //apply interest without service charge
}
}
}
return checkingBalance;
}
savingsAccount:: savingsAccount()
{
interestRate = .04;
}
void savingsAccount:: prePrint(int savingsAccountNumber, char accountType, double savingsBalance)
{
cout << "Account Number:" << savingsAccountNumber << " account type:" << accountType << " Starting Balance:" << savingsBalance << endl;
}
主:
#include "header.h"
int main()
{
ifstream inFile;
int numberOfAccounts, accountNumTemp, transactionNum, amountTemp;
double balanceTemp;
char discard, accountType, transactionTypeTemp;
bankAccount ba;
cout << "Processing account data..." << endl;
inFile.open("Bank.txt");
if (!inFile)
{
for (int a = 0; a < 20; a++)
cout << endl;
cout << "Cannot open the input file."
<< endl;
return 1;
}
inFile >> numberOfAccounts;
inFile.get(discard);
for (int i = 0; i < numberOfAccounts; i++)
{
inFile >> accountNumTemp >> accountType >> balanceTemp >> transactionNum;
inFile.get(discard);
ba.setAccountInfo(accountNumTemp, balanceTemp);
ba.prePrint(accountType);
for (int j = 0; j < transactionNum; j++)
{
inFile >> transactionTypeTemp >> amountTemp;
inFile.get(discard);
ba.process(accountType, transactionTypeTemp, amountTemp, j);
}
ba.postPrint();
}
inFile.close();
return 0;
}
這是一個可怕的很多代碼來幫助完成家庭作業 - 您是否嘗試過調試您的代碼?你期望發生什麼事情會發生什麼偏離?仔細檢查該行及其中使用的變量。另外,代碼中有很多地方不必要地重複行 - 例如'int checkingAccountNumber = accountNumber;'和'double checkingBalance = balance;' - 這些行可以移到if語句之上,從而減少代碼並使其生成更容易維護。 瞭解[DRY](http://en.wikipedia.org/wiki/DRY) – Basic 2011-04-24 02:28:31
@basiclife,是的,我知道它的延伸有人會挖掘所有的代碼,並找出問題。我以爲我今晚會發布這一切,以防某人感到無聊,並可以在我明天再次開始工作之前提供一些指示。 :P – darko 2011-04-24 02:35:55
1)這段代碼不能編譯,所以它實際上不是給你帶來麻煩的代碼,2)你沒有包含導致錯誤的數據('Bank.txt'),最重要的是所有,3)這個代碼太複雜了。你有兩個派生類,*這兩個都有問題*。你在注意到錯誤之前還是之後寫了第二堂課?永遠不要添加到不起作用的代碼。現在看看你能夠簡化這些代碼並且仍然得到錯誤,然後看看問題的原因是否會突然出現在你身上,如果沒有,那麼編輯你的問題。 – Beta 2011-04-24 04:14:12