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;
Re: Help with C++ project
Thanks that actually makes sense! Except when I set account[accountNumber] to new Savings how do I initialize Savings.
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"!
Re: Help with C++ project
Alright I'll try giving that a shot!
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.
Re: Help with C++ project
With pointers, you use "->" rather than ".".
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;
}
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.
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.
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?
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?
Re: Help with C++ project
Quote:
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.
Quote:
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
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);
Re: Help with C++ project
All pointers are dereferenced the same way.