帮助处理类和派生类
Help with class and derived class processing
我正在处理一个使用基类"bankAccount"和两个派生类"checkingAccount"answers"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;
}
我实际上在一家银行工作,所以我不能离开这个。: -)
增加你的问题:
if (transactionTypeTemp == 'D')
{
checkingBalance = checkingBalance + amountTemp;
checkingBalance = (checkingBalance * interestRate);
}
这个实际上只有在账户上留下了利息!
而且,真正的银行在你存钱的时候不会计算利息,而是按固定的日期计算,比如每月一次或每年一次。你得到(或支付)的利息也取决于账户有一定余额的天数。
if ((checkingBalance = checkingBalance - amountTemp) < 0)
{
cout << "error: transaction number" << j + 1 << " never occured due to insufficent funds." << endl;
}
尽管将文本写入cout,事务确实已经发生,因为=
给Balance分配了一个新值!也许你应该比较一下余额和金额?
然后在else
部分中再次重复无效利息计算。
有很多问题。我举一个例子。您有一个基类(银行帐户)和两个派生类(支票帐户和储蓄帐户)。然后在基类中有方法(process方法),这些方法从派生类实例化对象。这可不妙。我想重新复习一下派生类可以帮助你解决这个问题。
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 需要帮助设置在C++中使用的Potrace
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 如何委托派生类使用其父构造函数?
- 在指针的帮助下,文本文件中单词的频率
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 存储模板类型以强制转换回派生<T>
- 需要从 istream 和 ostream 派生 iostream
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- 在 C++ 中用派生类型重写成员函数
- 帮助处理类和派生类