Balance and interestRate shouldn't be pointers. They should be doubles themselves. There are several other problems in this code but I think you'll find them when you test it.
In deposit() and withdraw() I've used the += and -= equal operators.
1 2
balance += dollars; // same as balance = balance + dollars;
balance -= dollars; // same as balance = balance - dollars;
The requirements are to have balance and interestRate as double * Could you please use double * balance, interestRate . That is the whole point of my question. I dont know how to use the pointers and ignoring them is not answering my question. Sorry
It's OK not to like the answer, denying it is a different kettle of fish altogether.
FWIW a well established C++ teacher, Savitch, Ch10 where this problem and code originates, doesn't use pointers.
#include <iostream>
class Test
{
private:
double* value;
public:
Test() { value = new(double)(0); } // create on the heap and set to zero
~Test() { delete value; } // heap memory must be cleaned up
void AddFive() { *value += 5; }
double GetValue() const { return *value; }
};
int main()
{
Test t1;
std::cout << t1.GetValue() << '\n';
t1.AddFive();
std::cout << t1.GetValue() << '\n';
Test* t2 = new(Test);
std::cout << t2->GetValue() << '\n';
t2->AddFive();
std::cout << t2->GetValue() << '\n';
delete t2;
}
AFAIK the proper way to have a data member that is a pointer is to point the member to an address on the heap when the class is constructed. That requires the allocated memory to be cleaned up in the destructor.
AFAIK the proper way to have a data member that is a pointer is to point the member to an address on the heap when the class is constructed. That requires the allocated memory to be cleaned up in the destructor.
@rnima Please DON'T delete your question once you've gotten an answer. It makes the thread useless as a learning resource for other readers. It's selfish, and is an abuse of this forum.