기본 클래스 "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
@basiclife, 네, 저는 그 스트레치를 알고 있습니다. 누군가가 모든 코드를 파헤 치고 문제를 파악할 것입니다. 누군가가 지루해하고 내일 다시 작업하기 전에 몇 가지 조언을 해줄 수 있기 때문에 나는 오늘 밤 모든 것을 올릴 것이라고 생각했습니다. : P – darko
1)이 코드는 컴파일되지 않으므로 실제로 문제가되는 코드가 아닙니다. 2) 오류가 발생하는 데이터 ('Bank.txt')가 포함되어 있지 않습니다. 모두 3)이 코드는 너무 복잡합니다. 둘 모두 파손 된 * 파생 클래스가 두 개 있습니다.오류를 발견하기 전이나 후에 두 번째 수업을 작성 했습니까? 작동하지 않는 코드에는 절대로 추가하지 마십시오. 이제이 코드를 간소화하고 오류를 계속 가져올 수 있는지 확인한 다음 문제의 원인이 당신에게로 흘러 나오는지 확인한 다음 질문을 편집하십시오. – Beta