Hey I have a project for school where I have to make a bank class program using classes. I have to make a base class named Account and then 3 derived classes, Checking, Savings, and Loan. The problem I have is this program is read from a file our teacher created, so in main I have a while loop going through the file and if it reads an s it makes a savings object, if it is l it makes a loan object, etc. This is working fine but this objects also take in an account number, so transactions can be processed later. Here is the code for main:
Code:
main()
{
ifstream in;
char transactionType;
int accountNumber;
string accountName;
double initialBalance;
double theInterestRate;
double theCheckFee;
double theTransactionAmount;
string theAccountType;
in.open("hello.txt");
if (in.fail())
cout << "Error reading file";
Account account[5];
while (!in.eof())
{
in >> transactionType;
if (transactionType == 's')
{
in >> accountNumber;
in >> accountName;
in >> initialBalance;
in >> theInterestRate;
Savings savingAccount(accountNumber, accountName, initialBalance,
theInterestRate);
cout << "new account..." << endl << accountName << " (savings)";
cout << savingAccount;
account[accountNumber] = savingAccount;
}
else if (transactionType == 'c')
{
in >> accountNumber;
in >> accountName;
in >> initialBalance;
in >> theCheckFee;
Checkings checkingAccount(accountNumber, accountName, initialBalance,
theCheckFee);
cout << "new account..." << endl << accountName << " (checking)";
cout << checkingAccount;
account[accountNumber] = checkingAccount;
}
else if (transactionType == 'l')
{
theAccountType = "loan";
in >> accountNumber;
in >> accountName;
in >> initialBalance;
in >> theInterestRate;
initialBalance = -(initialBalance);
Loan loanAccount(accountNumber, accountName, initialBalance,
theInterestRate, theAccountType);
cout << "new account..." << endl << accountName << " (loan)";
cout << loanAccount;
account[accountNumber] = loanAccount;
}
else if (transactionType == 't')
{
in >> accountNumber;
in >> theTransactionAmount;
account[accountNumber].addTransaction(theTransactionAmount);
cout << account[accountNumber];
}
else if (transactionType == 'u')
{
in >> accountNumber;
}
else
cout << "Invalid Transaction Type";
}
So I save the loan, savings, or checking objects back into an account array so I can sort through the accounts in the transaction part of the function. But then I am changing the Account object and the loan, checkings, or savings, aren't even taken in at all. And since transactions are different depending on if it is a loan, savings, or checking, it doesn't work. So how would I make this work? Virtual functions? Or is there a way to point to my loan, savings, or checking account with an account number without actually changing the values. Idk. I am confused so I'm sure I'm not making any sense.
You're experiencing what is called object slicing. Since your array is of type Account, it isn't capable of storing the different account types. You need to store an array, or better a vector or Account pointers, then create the sub account objects using new, instead of using local variables inside your if statements.
You'd have something like
Code:
Account* account[5];
account[accountNumber] = new Savings;
Either give Savings a constructor taking the necessary arguments, or else you can initially hold it in a local Savings* and then assign that into the account array once you're done setting it up.
Don't forget that ever "new" must eventually have a corresponding "delete"!
Alright so that whole deal worked. But I came across another problem. In my transaction if statement it says,
Code:
else if (transactionType == 't')
{
in >> accountNumber;
in >> theTransactionAmount;
account[accountNumber].addTransaction(theTransactionAmount);
cout << account[accountNumber];
}
And when I try to compile it I get an error that says "error: request for member âaddTransactionâ in âaccount[accountNumber]â, which is of non-class type âAccount*â"
Why is this? Oh and addTransaction is of type Account.
Alright Awesome that worked! Now just one more problem (hopefully).
When I use cout in my transaction if statement, it outputs a bunch of hexadecimal numbers. Why does it do this? And I have the << operator overloaded and it works in my other if statements. It is
It works when I output a normal object but when I output the pointer it gets messed up. Is there a reason for this? Do I need to set the pointer object to something?
Bookmarks