I was assigned this homework.
Create a class BankAccount. The class contains balance(non negative double),
rate(non negative double) and account_id which is a 12 digit number.
1)Create a default constructor(balance=0,rate=0, account_id=111111111111)
2)Create a constructor that accepts parameters for all the features of the class.
3)Provide class with get_ methods for feature access to the class.
4)Provide class with set_ methods for feature change to the class.
4)Provide a update() method that adds an 1 year interest to the balance.
Your points are totally correct.
I updated my code.
But it seems i get weird outputs again.
I cannot get to work this:
4)Provide a update() method that adds an 1 year interest to the balance.
Any ideas?
I really appreciate your help. :)
I think you miss the point. Your object contains junk prior to and without calling get_update. Calling get_update while your object contains junk is clearly not going to be productive. I'm sure you've heard the phrase "garbage in, garbage out."
Your point is right. It's not the update method that's causing the problem.
I updated my code and it looks fine to me.
I literally don't know where the problem is. :(
This is the definition of the constructor you call on line 55. As you can see, the constructor isn't actually doing anything, and the member variables remain unchanged from their initial garbage state. Since the member variables aren't initialized, any get-methods accessing them will return garbage.
Make use of the this pointer. Since you've chosen to give the exact same names of the member variables to the arguments of the constructor, you'll have to be explicit.
Or, you could just change the names to avoid the ambiguity:
Thank you so much!! You are a lifesaver.
I didn't notice these minor mistakes.
Now my program works.
The only problem is that i cannot get the update function to work.
Any ideas?
Your update-getter isn't working for the same reason that your other getters didn't work before: You didn't initialize the members that the get-methods were accessing.
One look at your constructor(s) reveals that the member variable update was never initialized, and contains garbage.
In addition, and this is unrelated, your set_update() method doesn't actually "set" update to anything - it just returns what should have presumably been assigned to update, and then returned.
The code is updated!
I also put restrictions(non negative balance and rate).
Also i created an update funtion.
If you run the code you can see that if balance is negative it displays the warning message and the value. Also the update function outputs some weird value.
If you run the code you can see that if balance is negative it displays the warning message and the value.
Did you want it not to? It's doing that because the message is printed in the constructor, but later on the get_balance() method is invoked on line 69.
Also the update function outputs some weird value.
That's because the update() function is doing math with the member variable balance. The balance never gets initialized if the _balance passed into the constructor is less than zero. If you do anything with uninitialized garbage variables, you just make more garbage.
Also, your update function's signature indicates that it returns a double, but it doesn't actually return anything.