-
March 8th, 2011, 01:50 PM
#1
Help with C++ project
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.
-
March 8th, 2011, 01:59 PM
#2
Re: Help with C++ project
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;
-
March 8th, 2011, 03:14 PM
#3
Re: Help with C++ project
Thanks that actually makes sense! Except when I set account[accountNumber] to new Savings how do I initialize Savings.
-
March 8th, 2011, 03:20 PM
#4
Re: Help with C++ project
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"!
-
March 8th, 2011, 03:59 PM
#5
Re: Help with C++ project
Alright I'll try giving that a shot!
-
March 8th, 2011, 10:36 PM
#6
Re: Help with C++ project
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.
-
March 8th, 2011, 11:53 PM
#7
Re: Help with C++ project
With pointers, you use "->" rather than ".".
-
March 9th, 2011, 12:58 AM
#8
Re: Help with C++ project
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
Code:
ostream& operator << (ostream &out, const Account &account1)
{
cout.setf(ios::fixed);
cout.precision(2);
out << ": balance is $" << account1.initialBalance << endl << endl;
return out;
}
-
March 9th, 2011, 01:01 AM
#9
Re: Help with C++ project
Well, for starters, you're calling setf and setprecision on cout, but actually operating on out---not necessarily the same stream. Fix that.
I don't know why the output would look like hex though, from what you've shown.
-
March 9th, 2011, 01:08 AM
#10
Re: Help with C++ project
Alright I have virtual functions that look like this:
Code:
double Account::addTransaction(double theTransactionAmount)
{
initialBalance += theTransactionAmount;
return initialBalance;
}
double Loan::addTransaction(double theTransactionAmount)
{
initialBalance -= theTransactionAmount;
return initialBalance;
}
double Checkings::addTransaction(double theTransactionAmount)
{
initialBalance += theTransactionAmount;
return initialBalance;
}
double Savings::addTransaction(double theTransactionAmount)
{
initialBalance += theTransactionAmount;
return initialBalance;
}
If that has anything to do with it. Thanks for helping by the way.
-
March 9th, 2011, 01:27 AM
#11
Re: Help with C++ project
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?
-
March 9th, 2011, 04:15 AM
#12
Re: Help with C++ project
Alright so I think it's just that my overloaded extraction operator isn't working with the pointer object. Is there a way I can get it to work?
-
March 9th, 2011, 05:13 AM
#13
Re: Help with C++ project
Originally Posted by prestonprice57
It works when I output a normal object but when I output the pointer it gets messed up.
Of course, because you're doing as you stated -- you're outputting a pointer. A pointer is just an integral value.
Do I need to set the pointer object to something?
Pointers must always point to something before you use them.
Dereference the pointer, and you get the object you want to output:
Code:
#include <iostream>
int main()
{
int i = 10;
int *pi = &i; // pi is pointing to the i variable
std::cout << *pi;
}
You see that 10 is printed.
Regards,
Paul McKenzie
-
March 9th, 2011, 11:41 AM
#14
Re: Help with C++ project
Alright that makes sense. But how do I dereference a pointer to an object? Cause right now I have,
Code:
account[accountNumber] = new Loan(accountNumber, accountName,
initialBalance, theInterestRate);
-
March 9th, 2011, 11:45 AM
#15
Re: Help with C++ project
All pointers are dereferenced the same way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|